btn.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. "use strict";
  2. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  3. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  4. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  5. var utils = require('../../api/utils');
  6. var processDataSet = require('../utils/processDataSet');
  7. Component({
  8. data: {
  9. isUpdating: false,
  10. scope: '',
  11. getAuthorize: '',
  12. styleV2: my.styleV2
  13. },
  14. props: {
  15. size: 'default',
  16. type: '',
  17. plain: false,
  18. disabled: false,
  19. loading: false,
  20. 'form-type': '',
  21. 'open-type': '',
  22. 'app-parameter': '',
  23. 'hover-class': 'button-hover',
  24. 'hover-stop-propagation': false,
  25. 'hover-start-time': false,
  26. className: '',
  27. onTap: function onTap() {},
  28. onGetUserInfo: '',
  29. onGetPhoneNumber: ''
  30. },
  31. onInit: function onInit() {
  32. this.updateData();
  33. },
  34. deriveDataFromProps: function deriveDataFromProps(nextProps) {
  35. var size = nextProps.size,
  36. type = nextProps.type,
  37. plain = nextProps.plain,
  38. disabled = nextProps.disabled,
  39. loading = nextProps.loading,
  40. formType = nextProps.formType,
  41. openType = nextProps.openType,
  42. hoverClass = nextProps.hoverClass,
  43. hoverStopPropagation = nextProps.hoverStopPropagation,
  44. hoverStartTime = nextProps.hoverStartTime,
  45. appParameter = nextProps.appParameter;
  46. var opentype = this.getOpenType(openType);
  47. var scope = this.getScope(openType);
  48. var params = {
  49. size: size,
  50. type: type,
  51. plain: plain,
  52. disabled: disabled,
  53. loading: loading,
  54. formType: formType,
  55. opentype: opentype,
  56. scope: scope,
  57. hoverClass: hoverClass,
  58. hoverStopPropagation: hoverStopPropagation,
  59. hoverStartTime: hoverStartTime,
  60. appParameter: appParameter
  61. };
  62. Object.keys(params).forEach(function (key) {
  63. return params[key] === undefined && delete params[key];
  64. });
  65. this.setData(params);
  66. },
  67. methods: {
  68. updateData: function updateData() {
  69. var _this = this;
  70. if (this.data.isUpdating) {
  71. return;
  72. }
  73. this.setData({
  74. isUpdating: true
  75. });
  76. for (var key in this.props) {
  77. if (this.props.hasOwnProperty(key)) {
  78. typeof this.props[key] === 'string' && (this.props[key] = this.props[key].replace(/(^\s*)|(\s*$)/g, ''));
  79. }
  80. }
  81. var _this$props = this.props,
  82. size = _this$props.size,
  83. type = _this$props.type,
  84. plain = _this$props.plain,
  85. disabled = _this$props.disabled,
  86. loading = _this$props.loading,
  87. formType = _this$props.formType,
  88. hoverClass = _this$props.hoverClass,
  89. hoverStopPropagation = _this$props.hoverStopPropagation,
  90. hoverStartTime = _this$props.hoverStartTime,
  91. appParameter = _this$props.appParameter,
  92. openType = _this$props.openType;
  93. this.getSystem(function () {
  94. var opentype = _this.getOpenType(openType);
  95. var scope = _this.getScope(openType);
  96. _this.setData({
  97. isUpdating: false,
  98. size: size,
  99. type: type,
  100. plain: plain,
  101. disabled: disabled,
  102. loading: loading,
  103. formType: formType,
  104. opentype: opentype,
  105. hoverClass: hoverClass,
  106. hoverStopPropagation: hoverStopPropagation,
  107. hoverStartTime: hoverStartTime,
  108. appParameter: appParameter,
  109. scope: scope
  110. });
  111. });
  112. },
  113. getSystem: function getSystem(cb) {
  114. var that = this;
  115. my.getSystemInfo({
  116. success: function success(res) {
  117. var app = '';
  118. if (res.app && res.app === 'amap') {
  119. app = 'amap';
  120. } else {
  121. app = 'alipay';
  122. }
  123. that.setData({
  124. app: app
  125. });
  126. cb();
  127. }
  128. });
  129. },
  130. getOpenType: function getOpenType(opentype) {
  131. var transformList = {
  132. getPhoneNumber: 'getAuthorize',
  133. getUserInfo: 'getAuthorize'
  134. };
  135. if (transformList[opentype]) {
  136. return transformList[opentype];
  137. }
  138. var allowList = ['share', 'launchApp', 'getAuthorize', 'openSetting'];
  139. if (this.data.app === 'amap') {
  140. allowList = ['share', 'getAuthorize', 'openSetting'];
  141. }
  142. if (opentype) {
  143. if (allowList.indexOf(opentype) !== -1) {
  144. return opentype;
  145. } else {
  146. utils.warn("\u5C0F\u7A0B\u5E8Fopen-type\u503C\u4E0D\u652F\u6301".concat(opentype), {
  147. apiName: "button/open-type/".concat(opentype),
  148. errorType: 0,
  149. type: 'component'
  150. });
  151. }
  152. }
  153. return '';
  154. },
  155. getScope: function getScope(opentype) {
  156. var scopeMap = {
  157. getPhoneNumber: 'phoneNumber',
  158. getUserInfo: 'userInfo'
  159. };
  160. return scopeMap[opentype] || '';
  161. },
  162. onError: function onError(err) {
  163. if (this.props.onError === 'function') {
  164. this.props.onError(err);
  165. }
  166. },
  167. getAuthorize: function getAuthorize() {
  168. var that = this;
  169. var resObj = {};
  170. if (this.data.opentype === 'getAuthorize' && this.data.scope === 'phoneNumber') {
  171. my.getPhoneNumber({
  172. success: function success(res) {
  173. if (typeof that.props.onGetPhoneNumber === 'function') {
  174. resObj.detail = res;
  175. resObj.type = 'getphonenumber';
  176. that.props.onGetPhoneNumber(resObj);
  177. }
  178. },
  179. fail: function fail(res) {
  180. if (typeof that.props.onGetPhoneNumber === 'function') {
  181. resObj.detail = res;
  182. resObj.type = 'getphonenumber';
  183. that.props.onGetPhoneNumber(resObj);
  184. }
  185. }
  186. });
  187. }
  188. if (this.data.opentype === 'getAuthorize' && this.data.scope === 'userInfo') {
  189. my.getOpenUserInfo({
  190. success: function success(res) {
  191. if (typeof that.props.onGetUserInfo === 'function') {
  192. var _res = JSON.parse(res.response).response;
  193. _res.gender && _res.gender === 'm' ? _res.gender = 1 : _res.gender = 2;
  194. _res.avatarUrl = _res.avatar;
  195. delete _res.avatar;
  196. resObj.detail = {};
  197. resObj.detail.userInfo = _res;
  198. resObj.type = 'getuserinfo';
  199. that.props.onGetUserInfo(resObj);
  200. }
  201. },
  202. fail: function fail(res) {
  203. if (typeof that.props.onGetUserInfo === 'function') {
  204. resObj.detail = res;
  205. resObj.type = 'getuserinfo';
  206. that.props.onGetUserInfo(resObj);
  207. }
  208. }
  209. });
  210. }
  211. },
  212. stopEvent: function stopEvent() {},
  213. btnOnTap: function btnOnTap(e) {
  214. var that = this;
  215. var tapEvent = processDataSet(e, this.props);
  216. if (this.props.openType === 'openSetting') {
  217. my.openSetting({
  218. success: function success(res) {
  219. if (typeof that.props.onOpenSetting === 'function') {
  220. that.props.onOpenSetting(_objectSpread(_objectSpread({}, tapEvent), {}, {
  221. type: 'opensetting',
  222. detail: {
  223. authSetting: utils.mapAuthSetting(res.authSetting)
  224. }
  225. }));
  226. }
  227. }
  228. });
  229. }
  230. this.props.catchTap && this.props.catchTap(tapEvent);
  231. this.props.onTap && this.props.onTap(tapEvent);
  232. },
  233. getPhone: function getPhone(e) {
  234. var _this2 = this;
  235. var eve = _objectSpread({}, e);
  236. my.getPhoneNumber({
  237. success: function success(res) {
  238. if (typeof res.response === 'string') {
  239. var response = JSON.parse(res.response);
  240. if (response.response.code === '40001') {
  241. utils.warn('请去小程序开发管理后台的功能列表中添加获取电话功能', {
  242. apiName: 'button/bindgetphonenumber',
  243. errorType: 1,
  244. type: 'component'
  245. });
  246. }
  247. return false;
  248. }
  249. eve.detail = res.response;
  250. if (typeof _this2.props.onGetPhoneNumber === 'function') {
  251. _this2.props.onGetPhoneNumber(eve);
  252. }
  253. },
  254. fail: function fail(err) {
  255. throw err;
  256. }
  257. });
  258. },
  259. getUserInfo: function getUserInfo(e) {
  260. var that = this; // 获取用户信息
  261. var eve = _objectSpread({}, e);
  262. my.getAuthCode({
  263. scopes: 'auth_user',
  264. success: function success() {
  265. my.getOpenUserInfo({
  266. success: function success(userInfo) {
  267. eve.detail = {
  268. userInfo: {},
  269. rawData: ''
  270. };
  271. if (typeof userInfo.response === 'string') {
  272. var response = JSON.parse(userInfo.response);
  273. if (response.response.code === '40006') {
  274. utils.warn('请去小程序开发管理后台的功能列表中添加会员信息功能', {
  275. apiName: 'button/bindgetuserinfo',
  276. errorType: 1,
  277. type: 'component'
  278. });
  279. }
  280. return false;
  281. }
  282. eve.detail.userInfo = _objectSpread({}, userInfo.response);
  283. eve.detail.userInfo.avatarUrl = eve.detail.userInfo.avatar;
  284. delete eve.detail.userInfo.avatar;
  285. eve.detail.rawData = JSON.stringify(eve.detail.userInfo);
  286. if (typeof that.props.onGetUserInfo === 'function') {
  287. that.props.onGetUserInfo(eve);
  288. }
  289. },
  290. fail: function fail(err) {
  291. throw err;
  292. }
  293. });
  294. }
  295. });
  296. }
  297. }
  298. });