UNPKG

fastlion-amis

Version:

一种MIS页面生成工具

325 lines (324 loc) 16.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ScanCodeRenderer = void 0; var tslib_1 = require("tslib"); /** * 移动端扫码组件 */ var icons_1 = require("@ant-design/icons"); var antd_1 = require("antd"); var react_1 = (0, tslib_1.__importDefault)(require("react")); var factory_1 = require("../factory"); var helper_1 = require("../utils/helper"); var shell_1 = require("../utils/shell"); var msgsub_1 = (0, tslib_1.__importDefault)(require("../renderers/Lion/utils/msgsub")); var Scoped_1 = require("../Scoped"); var Modal_1 = (0, tslib_1.__importDefault)(require("antd/lib/modal/Modal")); var Input_1 = (0, tslib_1.__importDefault)(require("antd/lib/input/Input")); var tools_1 = require("../utils/shell/tools"); var shell_2 = require("../utils/shell/shell"); var react_dom_1 = require("react-dom"); var RfidUtil_1 = require("../utils/RfidUtil"); var keyStep_1 = require("../utils/keyStep"); var ScanCode = /** @class */ (function (_super) { (0, tslib_1.__extends)(ScanCode, _super); function ScanCode(props) { var _this = _super.call(this, props) || this; _this.startX = 0; _this.startY = 0; _this.hash = ''; _this.initData = function () { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var env, res, corpId, sysConfig, data; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: env = this.props.env; return [4 /*yield*/, env.fetcher({ url: '/api/v1/js/api/sdk', method: 'post' }, { jsSdkUrl: location.href.split('#')[0] })]; case 1: res = _a.sent(); if (res.status == 0 && res.data != null) { corpId = res.data.corpId; if (corpId) { sysConfig = res.data; data = { debug: false, appId: sysConfig.corpId, timestamp: sysConfig.timestamp, nonceStr: sysConfig.noncestr, signature: sysConfig.signature, jsApiList: ['scanQRCode', 'checkJsApi'] }; wx.config(data); wx.error(function (res) { antd_1.message.warn((res === null || res === void 0 ? void 0 : res.errMsg) || '鉴权失败'); }); } } else { antd_1.message.error(res.msg); } return [2 /*return*/]; } }); }); }; _this.dealCode = function (codeValue) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var _a, reload, valueAttr, env, api, data, ctx, payload, ctx_1, error_1; var _b; return (0, tslib_1.__generator)(this, function (_c) { switch (_c.label) { case 0: _a = this.props, reload = _a.reload, valueAttr = _a.valueAttr, env = _a.env, api = _a.api; _c.label = 1; case 1: _c.trys.push([1, 5, , 6]); data = (_b = {}, _b[valueAttr !== null && valueAttr !== void 0 ? valueAttr : 'scanValue'] = codeValue, _b); ctx = (0, helper_1.createObject)(this.props.data, data); return [4 /*yield*/, env.fetcher({ url: api.url, method: 'post', data: api.data }, ctx)]; case 2: payload = _c.sent(); if (!(payload.ok && payload.status == 20003)) return [3 /*break*/, 4]; return [4 /*yield*/, (0, keyStep_1.KeyStepCommonFunction)(payload.data.scanAfterDo, data, env)]; case 3: _c.sent(); _c.label = 4; case 4: if (payload.status === 0 || payload.status == 20003) { ctx_1 = (0, helper_1.createObject)(this.props.data, (0, tslib_1.__assign)((0, tslib_1.__assign)({}, payload.data), data)); reload && this.reloadTarget(reload, ctx_1); if (this.state.visible) { this.setState({ visible: false, codeValue: '' }); } antd_1.message.success(payload.msg); } return [3 /*break*/, 6]; case 5: error_1 = _c.sent(); msgsub_1.default._error('条码出错' + error_1); return [3 /*break*/, 6]; case 6: return [2 /*return*/]; } }); }); }; _this.handleScanCode = function () { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var _this = this; return (0, tslib_1.__generator)(this, function (_a) { if (shell_1.Shell.hasShell()) { shell_1.Shell.getScanCode().then(function (res) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { return (0, tslib_1.__generator)(this, function (_a) { if (res.success) { this.dealCode(res.data.content); } else { msgsub_1.default._info(res.msg); } return [2 /*return*/]; }); }); }); } else { if (tools_1.tools.isComWx) { this.getWorkScanCode(); } else { this.setState({ visible: true }); } } return [2 /*return*/]; }); }); }; //使用企微的扫一扫 _this.getWorkScanCode = function () { wx.scanQRCode({ desc: 'scanQRCode desc', needResult: 1, scanType: ["qrCode", "barCode"], success: function (res) { // 回调 if (res.resultStr) { //当needResult为1时返回处理结果 _this.dealCode(res.resultStr); } else { antd_1.message.error('无效码'); } }, error: function (res) { if (res.errMsg.indexOf('function_not_exist') > 0) { alert('版本过低请升级'); } else { alert(res.errMsg); } }, fail: function (res) { if (res.errMsg.indexOf('function_not_exist') > 0) { alert('版本过低请升级'); } else { alert(res.errMsg); } } }); }; //起始位置 _this.originX = 0; _this.originY = 0; //拖动过程中的临时位置 _this.movingX = 0; _this.movingY = 0; _this.touchStart = function (e) { e.persist(); e.preventDefault(); e.stopPropagation(); e.persist(); e.preventDefault(); e.stopPropagation(); if (e.touches.length > 0) { var touch = e.touches[0]; _this.originX = _this.movingX = touch.clientX; // 记录触摸开始的横坐标 _this.originY = _this.movingY = touch.clientY; // 记录触摸开始的纵坐标 } }; _this.touchMove = function (e) { e.persist(); e.preventDefault(); e.stopPropagation(); if (e.changedTouches.length > 0) { var touch = e.changedTouches[0]; var endX = touch.clientX; // 触摸结束的横坐标 var endY = touch.clientY; // 触摸结束的纵坐标 var moveX = endX - _this.movingX; // 横向移动距离 var moveY = endY - _this.movingY; // 纵向移动距离 if (moveX == 0 && moveY == 0) { } else { _this.movingX = touch.clientX; _this.movingY = touch.clientY; var nx = _this.state.position.x + moveX; var ny = _this.state.position.y - moveY; var dom = (0, react_dom_1.findDOMNode)(_this); if (nx <= 2) nx = 2; if (nx > dom.clientWidth - 280) nx = dom.clientWidth - 280; if (ny <= 2) ny = 2; if (ny > dom.clientHeight - 50) ny = dom.clientHeight - 50; _this.setState({ position: { x: nx, y: ny } }); } } }; _this.touchEnd = function (e) { // e.persist() // e.preventDefault() // e.stopPropagation() // if (e.changedTouches.length > 0) { // const touch = e.changedTouches[0]; // const endX = touch.clientX; // 触摸结束的横坐标 // const endY = touch.clientY; // 触摸结束的纵坐标 // // 如果拖动的距离很小,认为是一次单击事件 // const distance = Math.sqrt( // (endX - this.originX) ** 2 + (endY - this.originY) ** 2 // ); // if (distance < 1) { // // 在这里处理单击事件 // // this.handleScanCode() // } // } }; _this.state = { position: { x: (document.body.clientWidth - 280) / 2, y: 200 }, visible: false, codeValue: '' }; return _this; } ScanCode.prototype.componentDidMount = function () { var _this = this; // if (this.props.scanTriggerMode) // windowRFIDTriger.addEventListener(this.props.scanTriggerMode, this.handleScanCode) // message.success('我注册了') RfidUtil_1.windowRFIDTriger.addEventListener(this.props.scanTriggerMode || RfidUtil_1.RFIDTrigeerCodeEnum.TRIGGERCLICK, this.handleScanCode); tools_1.tools.isComWx && this.initData(); this.hash = window.location.hash.slice(1); this.codeEvent = shell_2.eventStation.subscription(shell_2.RuleTypes.ShellCode, function (_a) { var code = _a.code, href = _a.href; if (_this.hash == href) { _this.dealCode(code); } }); }; ScanCode.prototype.componentWillUnmount = function () { var _a; // if (this.props.scanTriggerMode) // windowRFIDTriger.removeEventListener(this.props.scanTriggerMode, this.handleScanCode) RfidUtil_1.windowRFIDTriger.removeEventListener(this.props.scanTriggerMode || RfidUtil_1.RFIDTrigeerCodeEnum.TRIGGERCLICK, this.handleScanCode); (_a = this.codeEvent) === null || _a === void 0 ? void 0 : _a.call(this); }; ScanCode.prototype.reloadTarget = function (target, data) { }; ScanCode.prototype.render = function () { var _this = this; var _a = this.props, render = _a.render, cx = _a.classnames, body = _a.body, actionLabel = _a.actionLabel; var _b = this.state, position = _b.position, visible = _b.visible, codeValue = _b.codeValue; return (react_1.default.createElement("div", { className: cx("Scan-code-container") }, (0, helper_1.isMobile)() && react_1.default.createElement("div", { className: cx("Scan-btn"), onTouchStart: this.touchStart, onTouchMove: this.touchMove, onTouchEnd: this.touchEnd, style: { display: 'flex', position: 'absolute', left: position.x, bottom: position.y } }, react_1.default.createElement(antd_1.Button, { type: "link", size: "large", style: { backgroundColor: '#3574ee', color: 'white', paddingLeft: 40, width: 176, height: 46, borderTopLeftRadius: 40, borderBottomLeftRadius: 40 }, onClick: function (e) { e.stopPropagation(); _this.handleScanCode(); }, icon: react_1.default.createElement(icons_1.ScanOutlined, null) }, (actionLabel === null || actionLabel === void 0 ? void 0 : actionLabel.codeLabel) || '点击扫码'), react_1.default.createElement("div", { style: { position: 'absolute', left: 175, zIndex: 1, width: 24, height: 46, backgroundColor: '#3574ee', borderTopRightRadius: 40, borderBottomRightRadius: 40 } }), react_1.default.createElement(antd_1.Button, { type: "link", size: "large", style: { backgroundColor: 'rgba(0,0,0,0.26)', color: 'white', paddingLeft: 30, borderColor: 'transparent', width: 104, height: 46, borderTopRightRadius: 40, borderBottomRightRadius: 40 }, onClick: function (e) { e.stopPropagation(); _this.setState({ visible: true }); } }, "\u8F93\u5165")), render('scan-code', body), visible && react_1.default.createElement(Modal_1.default, { className: "scan-code-modal", visible: visible, closable: false, okText: "\u786E\u5B9A", cancelText: "\u53D6\u6D88", width: 320, style: { top: 200 }, maskClosable: false, onCancel: function () { _this.setState({ visible: false, codeValue: '' }); }, zIndex: 1011, onOk: function () { console.log(codeValue); if (codeValue) { _this.dealCode(codeValue); _this.setState({ visible: false }); } else { antd_1.message.warn('请输入条码内容!'); } }, getContainer: this.props.env.getModalContainer }, react_1.default.createElement("div", { className: "code-input-container" }, react_1.default.createElement("div", { className: "title" }, "\u8BF7\u8F93\u5165\u6761\u7801\u5185\u5BB9"), react_1.default.createElement("div", { className: "input-box" }, react_1.default.createElement(Input_1.default, { onChange: function (e) { _this.setState({ codeValue: e.target.value }); }, autoFocus: true, value: codeValue })))))); }; return ScanCode; }(react_1.default.Component)); var ScanCodeRenderer = /** @class */ (function (_super) { (0, tslib_1.__extends)(ScanCodeRenderer, _super); function ScanCodeRenderer(props, context) { var _this = _super.call(this, props) || this; var scoped = context; scoped.registerComponent(_this); return _this; } ScanCodeRenderer.prototype.reloadTarget = function (target, data) { var scoped = this.context; scoped.reload(target, data); }; var _a; ScanCodeRenderer.contextType = Scoped_1.ScopedContext; ScanCodeRenderer = (0, tslib_1.__decorate)([ (0, factory_1.Renderer)({ type: 'scan-code', isolateScope: true, }), (0, tslib_1.__metadata)("design:paramtypes", [Object, typeof (_a = typeof Scoped_1.IScopedContext !== "undefined" && Scoped_1.IScopedContext) === "function" ? _a : Object]) ], ScanCodeRenderer); return ScanCodeRenderer; }(ScanCode)); exports.ScanCodeRenderer = ScanCodeRenderer; //# sourceMappingURL=./mobile/ScanCode.js.map