fa.mixin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. export const tools = {
  2. filters: {
  3. },
  4. computed: {
  5. },
  6. methods: {
  7. //富文本的回调
  8. navigate(e) {
  9. if (e.href && e.href.indexOf('http') == -1) { //不完整的链接
  10. // #ifdef MP
  11. this.$util.uniCopy({
  12. content: this.vuex_config.upload.cdnurl + e.href,
  13. success: () => {
  14. this.$u.toast('链接已复制,请在浏览器中打开')
  15. }
  16. })
  17. // #endif
  18. // #ifndef MP
  19. window.open(this.vuex_config.upload.cdnurl + e.href);
  20. // #endif
  21. }
  22. },
  23. //卡片跳转
  24. diylinkpress(e) {
  25. e.ignore();
  26. this.goPage(e.href);
  27. return false;
  28. },
  29. //预览图片
  30. lookImage(index) {
  31. uni.previewImage({
  32. current: index,
  33. urls: this.imagesList,
  34. longPressActions: {
  35. itemList: ['发送给朋友', '保存图片', '收藏'],
  36. success: function(data) {
  37. console.log(data)
  38. },
  39. fail: function(err) {
  40. console.log(err.errMsg);
  41. }
  42. }
  43. });
  44. },
  45. //复制url
  46. copyUrl(url = '') {
  47. this.$util.uniCopy({
  48. content: url || window.location.href,
  49. success: () => {
  50. this.$u.toast('复制成功,请去粘贴发送给好友吧');
  51. },
  52. error: () => {
  53. console.log('复制失败!')
  54. }
  55. })
  56. },
  57. //cdnurl
  58. cdnurl(url) {
  59. if (!/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/.test(url)) {
  60. return this.vuex_config.upload.cdnurl + url;
  61. }
  62. return url;
  63. },
  64. //页面跳转
  65. goPage(path, auth) {
  66. if (path == 'out') {
  67. this.$u.vuex('vuex_token', '');
  68. this.$u.vuex('vuex_user', {});
  69. this.$u.vuex('vuex_openid', '');
  70. return;
  71. }
  72. if (auth && !this.vuex_token) {
  73. let pages = getCurrentPages();
  74. // 页面栈中的最后一个即为项为当前页面,route属性为页面路径
  75. let lastPageUrl = pages[pages.length - 1].$page.fullPath;
  76. this.$u.vuex('vuex_lasturl', lastPageUrl);
  77. this.$u.route('/pages/login/mobilelogin');
  78. return;
  79. }
  80. uni.$u.route({
  81. url: path,
  82. complete(e) {
  83. console.log(e, path)
  84. }
  85. })
  86. },
  87. logistics(res) {
  88. // #ifdef MP-WEIXIN
  89. if (this.vuex_config.logisticstype == 'kd100') {
  90. wx.navigateToMiniProgram({
  91. appId: 'wx6885acbedba59c14',
  92. path: `pages/result/result?nu=${res.expressno}&com=&querysource=third_xcx`, //备注:nu为快递单号,com为快递公司编码,com没有可不传
  93. extraData: {
  94. foo: 'bar'
  95. },
  96. success: (res) => {
  97. // 打开成功
  98. }
  99. });
  100. } else {
  101. this.goPage(`/pages/order/logistics?nu=${res.expressno}&coname=${res.expressname}&order_sn=${res.order_sn}`);
  102. }
  103. // #endif
  104. // #ifndef MP-WEIXIN
  105. this.goPage(`/pages/order/logistics?nu=${res.expressno}&coname=${res.expressname}&order_sn=${res.order_sn}`);
  106. // #endif
  107. }
  108. }
  109. }
  110. //修改头像的事件
  111. export const avatar = {
  112. methods: {
  113. chooseAvatar() {
  114. uni.$on('uAvatarCropper', this.upload);
  115. this.$u.route({
  116. // 关于此路径,请见下方"注意事项"
  117. url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
  118. // 内部已设置以下默认参数值,可不传这些参数
  119. params: {
  120. // 输出图片宽度,高等于宽,单位px
  121. destWidth: 300,
  122. // 裁剪框宽度,高等于宽,单位px
  123. rectWidth: 300,
  124. // 输出的图片类型,如果'png'类型发现裁剪的图片太大,改成"jpg"即可
  125. fileType: 'jpg'
  126. }
  127. });
  128. },
  129. upload: async function(path) {
  130. uni.$off('uAvatarCropper', this.upload);
  131. // 可以在此上传到服务端
  132. try {
  133. let res = await this.$api.goUpload({
  134. filePath: path
  135. });
  136. if (!res.code) {
  137. this.$u.toast(res.msg);
  138. }
  139. this.form.avatar = res.data.url;
  140. this.url = res.data.fullurl;
  141. if (typeof this.editAvatar == 'function') {
  142. this.editAvatar();
  143. }
  144. } catch (e) {
  145. console.error(e);
  146. this.$u.toast('图片上传失败!');
  147. }
  148. }
  149. }
  150. }
  151. // 登录方法
  152. export const loginfunc = {
  153. methods: {
  154. // 登录成功
  155. async success(delta) {
  156. //重置用户信息
  157. let apptype = '';
  158. let platform = '';
  159. let logincode = '';
  160. // #ifdef MP-WEIXIN
  161. platform = 'wechat';
  162. apptype = 'miniapp';
  163. logincode = await this.getMpCode();
  164. // #endif
  165. this.$api.getUserIndex({ apptype, platform, logincode }).then(res => {
  166. if (res.code) {
  167. this.$u.vuex('vuex_user', res.data.userInfo);
  168. if (res.data.openid) {
  169. this.$u.vuex('vuex_openid', res.data.openid);
  170. }
  171. }
  172. console.log(delta);
  173. var pages = getCurrentPages();
  174. if (!delta) {
  175. delta = 0;
  176. for (let i = pages.length; i > 0; i--) {
  177. console.log(pages[i - 1].route);
  178. if (pages[i - 1].route.indexOf("pages/login/") == -1) {
  179. break;
  180. } else {
  181. delta++;
  182. }
  183. }
  184. //根据pages自动计算出的delta
  185. console.log(delta);
  186. }
  187. let url = this.vuex_lasturl || '/pages/index/index';
  188. //清空最后页面
  189. this.$u.vuex('vuex_lasturl', '');
  190. //不在H5
  191. // #ifndef H5
  192. if (typeof pages[delta] !== 'undefined') {
  193. uni.navigateBack({
  194. delta: delta
  195. });
  196. } else {
  197. uni.reLaunch({
  198. url: url
  199. });
  200. }
  201. // #endif
  202. // 在H5 刷新导致路由丢失
  203. // #ifdef H5
  204. //有上次页面,关闭所有页面,到此页面,是从授权的,授权页面被刷新过
  205. if (pages.length <= 1 || pages[0].route.match(/pages\/login\//)) {
  206. uni.reLaunch({
  207. url: url
  208. });
  209. } else {
  210. uni.navigateBack({
  211. delta: delta
  212. });
  213. }
  214. // #endif
  215. });
  216. },
  217. // #ifdef MP-WEIXIN
  218. // 获取手机号回调
  219. async getPhoneNumber(e) {
  220. if (e.detail.errMsg === "privacy permission is not authorized") {
  221. this.$refs.uToast.show({
  222. title: '授权失败,失败原因:未同意隐私协议',
  223. type: 'error'
  224. });
  225. return;
  226. }
  227. if (e.detail.errMsg === "privacy permission is not authorized in gap") {
  228. this.$refs.uToast.show({
  229. title: '授权失败,原因:未同意隐私协议,请稍后重试',
  230. type: 'error'
  231. });
  232. return;
  233. }
  234. if (e.detail.errMsg === "getPhoneNumber:fail user deny") {
  235. this.$refs.uToast.show({
  236. title: '授权失败,原因:用户拒绝',
  237. type: 'error'
  238. });
  239. return;
  240. }
  241. if (e.detail.errMsg !== "getPhoneNumber:ok") {
  242. this.$refs.uToast.show({
  243. title: '授权失败,原因:' + e.detail.errMsg,
  244. type: 'error'
  245. });
  246. return;
  247. }
  248. if (typeof this.agreeChecked !== 'undefined' && !this.agreeChecked) {
  249. this.$refs.uToast.show({
  250. title: '请阅读并同意遵守《用户协议》',
  251. type: 'error'
  252. });
  253. return;
  254. }
  255. let logincode = await this.getMpCode();
  256. this.$api.goWechatMobileLogin({
  257. code: e.detail.code,
  258. logincode: logincode,
  259. bind: this.is_bind || ''
  260. }).then((res) => {
  261. if (res.code == 1) {
  262. this.$u.vuex('vuex_token', res.data.token);
  263. this.$u.vuex('vuex_openid', res.data.openid);
  264. this.success();
  265. } else {
  266. this.$u.toast(res.msg);
  267. }
  268. });
  269. },
  270. // 获取微信小程序用户openid
  271. async getWechatOpenid() {
  272. let logincode = await this.getMpCode();
  273. let res = await this.$api.getWechatOpenid({
  274. logincode: logincode,
  275. });
  276. if (res.code && res.data.openid) {
  277. this.$u.vuex('vuex_openid', res.data.openid);
  278. return res.data.openid;
  279. }
  280. return '';
  281. },
  282. // #endif
  283. // #ifdef H5
  284. // 公众号授权
  285. async goAuth(page, scope) {
  286. if (this.$util.isWeiXinBrowser()) {
  287. page = page ? page : '/pages/login/auth';
  288. let url = window.location.origin + (window.location.hash != '' ?
  289. window.location.pathname + '?hashpath=' + page :
  290. window.location.pathname.replace(/\/pages\/.*/, page));
  291. let res = await this.$api.getAuthUrl({
  292. platform: 'wechat',
  293. url: url,
  294. scope: scope || "",
  295. });
  296. if (!res.code) {
  297. this.$u.toast(res.msg);
  298. return;
  299. }
  300. var pages = getCurrentPages();
  301. let len = pages.length;
  302. if (len > 1) {
  303. let url = pages[len - 1].route;
  304. if (url.indexOf('/login/') != -1) {
  305. //找到上一个不是登录页面
  306. for (let i = len - 1; i >= 0; i--) {
  307. if (pages[i].route.indexOf('/login/') == -1) {
  308. this.$u.vuex('vuex_lasturl', '/' + pages[i].route + this.$u.queryParams(pages[i].options));
  309. break;
  310. }
  311. }
  312. } else {
  313. this.$u.vuex('vuex_lasturl', '/' + url + this.$u.queryParams(pages[pages.length - 1].options))
  314. }
  315. }
  316. window.location.href = res.data;
  317. }
  318. },
  319. // #endif
  320. // #ifdef MP-WEIXIN
  321. // 获取登录Code
  322. async getMpCode() {
  323. return new Promise((resolve, reject) => {
  324. uni.login({
  325. success: function(res) {
  326. if (res.code) {
  327. resolve(res.code);
  328. } else {
  329. //login成功,但是没有取到code
  330. reject('未取得code');
  331. }
  332. },
  333. fail: function(res) {
  334. reject('用户授权失败wx.login');
  335. }
  336. });
  337. });
  338. },
  339. // 微信授权登录
  340. async goMpLogin() {
  341. uni.showLoading({ title: '登录中...' });
  342. let that = this;
  343. try {
  344. let code = await that.getMpCode();
  345. let data = {
  346. code: code,
  347. __token__: that.vuex__token__
  348. };
  349. //有推荐码的话带上
  350. if (that.vuex_invitecode) {
  351. data.invitecode = that.vuex_invitecode;
  352. }
  353. let res = await this.$api.gowxLogin(data);
  354. uni.hideLoading();
  355. if (!res.code) {
  356. this.$u.toast(res.msg);
  357. return;
  358. }
  359. if (res.data.user) {
  360. this.$u.vuex('vuex_token', res.data.user.token);
  361. this.success();
  362. return;
  363. }
  364. this.$u.vuex('vuex_third', res.data.third);
  365. //授权成功到登录或绑定页面
  366. this.$u.route('/pages/login/register?bind=bind');
  367. } catch (e) {
  368. uni.hideLoading();
  369. that.$u.toast(e);
  370. }
  371. },
  372. // #endif
  373. // #ifdef APP-PLUS
  374. // App登录
  375. goAppLogin() {
  376. let that = this;
  377. var all, Service;
  378. // 1.发送请求获取code
  379. plus.oauth.getServices(
  380. function(Services) {
  381. all = Services;
  382. Object.keys(all).some(key => {
  383. if (all[key].id == 'weixin') {
  384. Service = all[key];
  385. }
  386. });
  387. Service.authorize(
  388. async function(e) {
  389. let res = await that.$api.goAppLogin({
  390. code: e.code,
  391. scope: e.scope
  392. });
  393. if (!res.code) {
  394. that.$u.toast(res.msg);
  395. return;
  396. }
  397. if (res.data.user) {
  398. that.$u.vuex('vuex_token', res.data.user.token);
  399. that.$u.vuex('vuex_user', res.data.user || {});
  400. that.success();
  401. return;
  402. }
  403. that.$u.vuex('vuex_third', res.data.third);
  404. that.$u.vuex('vuex_openid', res.data.openid);
  405. that.$u.route('/pages/login/register?bind=bind');
  406. },
  407. function(e) {
  408. that.$u.toast('授权失败!');
  409. }
  410. );
  411. },
  412. function(err) {
  413. console.log(err);
  414. that.$u.toast('授权失败!');
  415. }
  416. );
  417. },
  418. // #endif
  419. // 判断是否允许对应的登录方式
  420. checkLogintype(type) {
  421. return this.vuex_config.logintypearr && this.vuex_config.logintypearr.indexOf(type) > -1;
  422. }
  423. }
  424. }