request.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. const app = getApp();
  2. let WebIM = (wx.WebIM = require("../hxChatSDK/utils/WebIM")["default"]);
  3. const env = {
  4. NODE_ENV: 'dev',
  5. dev: {
  6. apiUrl: 'https://rsapi.zhiqiyun.net'
  7. },
  8. prd: {
  9. apiUrl: 'https://apimall.zhiqiyun.com'
  10. },
  11. pota: {
  12. // apiUrl: 'http://192.168.110.98:8098'
  13. // apiUrl: 'http://192.168.110.180:8098' ,//f
  14. // apiUrl: 'http://192.168.110.182:8094' ,//z
  15. // apiUrl: 'http://192.168.110.176:8098' //y
  16. // apiUrl: 'http://192.168.110.117:8098'//l
  17. // apiUrl: 'http://192.168.110.216:8098'
  18. apiUrl: 'http://192.168.110.180:8098' ,//wjg
  19. }
  20. }
  21. var header = {
  22. 'content-type': 'application/x-www-form-urlencoded',
  23. 'appId': 'ZQhe229Ax49Pdpy', //中国人寿测试环境
  24. }
  25. /**小程序配置 */
  26. const publics = {
  27. picUrls: 'https://zhiqiyun.oss-cn-hangzhou.aliyuncs.com/static/', //图片地址
  28. 'copyrighttitle': '智企云', //底部版权公司名称
  29. 'copyrighttel': '400-698-5980', //底部版权服务电话
  30. 'copyrightlogo': 'zhiqiyun', //底部版权 logo 文件名
  31. 'yaomeng': 'https://cfwz.yaomengwang.cn/hunan?enterpriseCode=hnty&orderId=', //药盟接口地址
  32. 'storemanagers': 'https://websmall.zhiqiyun.com?appId=', //saas门店管理地址(智企云所有)
  33. 'channel': '', //处方中间渠道号 SAAS系统为空
  34. 'is_customer_pick_up': false,
  35. // 'adGroupId': '1196269897935630352', //享团-宜得了
  36. 'mapLBSKEY': 'YQZBZ-6LMC4-IGQUO-XE64O-4UJL6-YNB77',
  37. 'iconSrc': 'red/',
  38. 'titleTopBgColor': '#FFF',
  39. // 小程序ID wxe7bec198ecfe1162
  40. // 文章栏目code
  41. 'fxCode': 'faxian', //发现
  42. 'xbrmCode': 'xiaobairumen', //小白入门
  43. 'tbalCode': 'toubaoanli', //投保案例
  44. 'lpalCode': 'lipeianli', //理赔案例
  45. }
  46. /*
  47. * 获取token
  48. */
  49. const getToken = (success) => {
  50. let token = getStorage('AUTH_TOKEN');
  51. if (token) {
  52. success.call(this, token);
  53. return false;
  54. } else {
  55. loadToken(data => {
  56. success.call(this, data);
  57. });
  58. }
  59. }
  60. const loadToken = (success) => {
  61. let that = this
  62. uni.request({
  63. url: env[env.NODE_ENV].apiUrl + '/api/access_token',
  64. data: {},
  65. method: 'get',
  66. header: header,
  67. success(json) {
  68. if (json.statusCode !== 200) return msg(json.data.message);
  69. if (json.data.code !== 0) return msg(json.data.msg)
  70. setStorage('AUTH_TOKEN', json.data.data);
  71. success.call(that, json.data.data);
  72. },
  73. fail() {
  74. msg('access_token 获取失败');
  75. }
  76. })
  77. }
  78. /**
  79. * 微信小程序静默授权登录
  80. * @param {*} parentId
  81. * @param {*} merchantId
  82. */
  83. const silenceLogin = (parentId, merchantId) => {
  84. // #ifdef MP-WEIXIN
  85. return new Promise((resolve, reject) => {
  86. if (!isLogins(false)) { //未登录
  87. uni.login({
  88. success(res) {
  89. console.log('静默授权登录>>>', res);
  90. let params = {
  91. code: res.code,
  92. parentId: parentId ? parentId : '',
  93. merchantId: merchantId ? merchantId : ''
  94. }
  95. postRequest('/api/v3/silence/login', params, data => {
  96. if (data.token) setStorage('AUTH_TOKEN', data.token);
  97. getRequest('/api/user/info', {}, suc => {
  98. data.isDistriUser = suc.isDistriUser;
  99. setStorage('userInfo', data);
  100. getIMToken(false);
  101. });
  102. resolve();
  103. });
  104. }
  105. });
  106. } else {
  107. initIMChat();
  108. resolve();
  109. }
  110. });
  111. // #endif
  112. }
  113. /**
  114. * 初始化 IMsdk
  115. */
  116. const initIMChat = () => {
  117. var user = getStorage('userInfo');
  118. if (user) {
  119. var imToken = getStorage('imToken');
  120. if (imToken) {
  121. //初始化IM
  122. uni.setStorage({
  123. key: "myUsername",
  124. data: 'RS_' + user.id
  125. });
  126. getApp().globalData.conn.open({
  127. apiUrl: WebIM.config.apiURL,
  128. user: 'RS_' + user.id,
  129. pwd: getStorage('imToken'),
  130. accessToken: getStorage('imToken'),
  131. appKey: WebIM.config.appkey
  132. });
  133. } else {
  134. getRequest('/api/im/msg/userToken', {}, suc => {
  135. setStorage('imToken', suc);
  136. uni.setStorage({
  137. key: "myUsername",
  138. data: 'RS_' + user.id
  139. });
  140. getApp().globalData.conn.open({
  141. apiUrl: WebIM.config.apiURL,
  142. user: 'RS_' + user.id,
  143. pwd: getStorage('imToken'),
  144. accessToken: getStorage('imToken'),
  145. appKey: WebIM.config.appkey
  146. });
  147. });
  148. }
  149. }
  150. }
  151. const getIMToken = (isUpdatToken) => {
  152. var user = getStorage('userInfo');
  153. getRequest('/api/im/msg/userToken', {}, suc => {
  154. setStorage('imToken', suc);
  155. if (isUpdatToken) {
  156. //更新token
  157. uni.setStorage({
  158. key: "myUsername",
  159. data: 'RS_' + user.id
  160. });
  161. getApp().globalData.conn.open({
  162. user: 'RS_' + user.id,
  163. accessToken: getStorage('imToken'),
  164. });
  165. getApp().globalData.onLoginSuccess(uni.getStorageSync("myUsername"));
  166. } else {
  167. initIMChat();
  168. }
  169. }, true);
  170. }
  171. /**
  172. * 跳转进入消息列表
  173. */
  174. const jumpConversation = () => {
  175. if (!isLogins(true)) { //未登录
  176. return;
  177. }
  178. getIMToken(true);
  179. }
  180. const getWXUserProfile = (suc) => {
  181. let that = this;
  182. var sessionKey = '';
  183. var openid = '';
  184. getApp().globalData.getCheckSessoin(json => {
  185. sessionKey = json.session_key;
  186. openid = json.openid;
  187. });
  188. try {
  189. uni.getUserProfile({
  190. desc: '完善信息',
  191. success: res => {
  192. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  193. },
  194. fail: res => {
  195. console.log('授权失败数据>>>', res);
  196. msg(res);
  197. }
  198. });
  199. } catch (e) {
  200. // 老版
  201. wx.getUserInfo({
  202. success: function(ress) {
  203. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  204. }
  205. });
  206. }
  207. }
  208. /**
  209. * 保存微信头像
  210. */
  211. const updataWxInfos = (iv, encryptedData, sessionKey, openid, suc) => {
  212. if (!encryptedData || !iv) return false;
  213. let _params = {
  214. sessionKey: sessionKey,
  215. openId: openid,
  216. iv: iv,
  217. encryptedData: encryptedData
  218. };
  219. postRequest('/api/user/weixin/update', _params, json => {
  220. setStorage('userInfo', json);
  221. suc.call();
  222. });
  223. }
  224. const clearValueEmpty = (data) => {
  225. let keyValue = {};
  226. for (let key in data) {
  227. let value = typeof data[key];
  228. if (value == 'string' && value) {
  229. if (data[key] != 'undefined' && data[key] != " " && data[key] != undefined && data[key] != null) {
  230. keyValue[key] = data[key];
  231. }
  232. // keyValue[key] = data[key];
  233. } else if (value == 'number' && value != null) {
  234. keyValue[key] = data[key];
  235. } else if (value == 'boolean') {
  236. keyValue[key] = data[key];
  237. } else {
  238. if (data[key]) keyValue[key] = data[key];
  239. }
  240. }
  241. return keyValue;
  242. }
  243. /**
  244. * 公共请求
  245. *
  246. * @param {*} url 请求url
  247. * @param {*} data 请求参数
  248. * @param {*} method 请求方法
  249. * @param {*} success 成功函数
  250. * @param {*} isLoad 是否显示加载提示
  251. */
  252. const baseRequest = (url, data, method, success, isLoad) => {
  253. getToken(token => {
  254. if (!isLoad) uni.showNavigationBarLoading();
  255. header.authorityToken = token;
  256. // if (url.indexOf('/v3') != -1 || url.indexOf('/paper/create') != -1) {
  257. // header['content-type'] = 'application/json;charset=UTF-8'
  258. // } else {
  259. header['content-type'] = 'application/json;charset=UTF-8'
  260. // }
  261. uni.request({
  262. url: env[env.NODE_ENV].apiUrl + url,
  263. data: clearValueEmpty(data),
  264. method: method,
  265. header: header,
  266. success(json) {
  267. if (!isLoad) uni.hideNavigationBarLoading();
  268. if (json.statusCode !== 200) return msg(json.data.message);
  269. if (json.data.code === 10021 || json.data.code === 10020) {
  270. // console.log("第二次执行")
  271. removeStorage('AUTH_TOKEN');
  272. removeStorage('userInfo');
  273. return redirectIndex();
  274. }
  275. if (json.data.code !== 0) {
  276. return msg(json.data.msg)
  277. }
  278. // console.log("第二次执行")
  279. let data = json.data.data;
  280. if (typeof data === 'string' && data.indexOf("{") === 0) {
  281. data = JSON.parse(data);
  282. }
  283. success.call(this, data);
  284. },
  285. fail() {
  286. if (!isLoad) uni.hideNavigationBarLoading();
  287. },
  288. // complete(){
  289. // success.call(this, data);
  290. // },
  291. })
  292. })
  293. }
  294. const loadIng = (msg) => {
  295. uni.showLoading({
  296. title: msg,
  297. mask: true
  298. })
  299. }
  300. /**POST请求 */
  301. const postRequest = (url, data, success, isLoad) => {
  302. header['content-type'] = 'application/json;charset=UTF-8'
  303. baseRequest(url, data, 'post', success, isLoad);
  304. }
  305. const postRequestLoding = (url, data, success, isLoding) => {
  306. header['content-type'] = 'application/json;charset=UTF-8'
  307. baseRequest(url, data, 'post', success, false, isLoding);
  308. }
  309. /**GET请求 */
  310. const getRequest = (url, data, success, isLoad) => {
  311. header['content-type'] = 'application/json;charset=UTF-8'
  312. baseRequest(url, data, 'get', success, isLoad);
  313. }
  314. /**Put请求 */
  315. const putRequest = (url, data, success, isLoad) => {
  316. header['content-type'] = 'application/json;charset=UTF-8'
  317. baseRequest(url, data, 'put', success, isLoad)
  318. }
  319. const putRequestJson = (url, data, success, isLoad) => {
  320. header['content-type'] = 'application/json;charset=UTF-8'
  321. baseRequest(url, data, 'put', success, isLoad)
  322. }
  323. /**
  324. * 上传文件
  325. * @param {*} url 请求url
  326. * @param {*} data 携带数据
  327. * @param {*} success 请求成功函数
  328. */
  329. const uploadFile = (url, data, success) => {
  330. // console.log(env[env.NODE_ENV].apiUrl + url, data)
  331. getToken(token => {
  332. uni.uploadFile({
  333. url: env[env.NODE_ENV].apiUrl + url,
  334. filePath: data,
  335. name: 'file',
  336. header: {
  337. 'appId': header['appId'],
  338. 'authorityToken': token
  339. },
  340. success(res) {
  341. if (res.statusCode !== 200) return msg('文件上传失败');
  342. let data = JSON.parse(res.data);
  343. if (data.code !== 0) return msg(data.msg);
  344. success.call(this, data.data);
  345. },
  346. })
  347. })
  348. }
  349. /**弹窗 */
  350. const msg = (title, success) => {
  351. if (title) {
  352. uni.showToast({
  353. title: title,
  354. icon: 'none',
  355. success() {
  356. if (success) success.call(this);
  357. }
  358. });
  359. }
  360. return false;
  361. }
  362. const msgConfirm = (msg, success, cancel) => {
  363. uni.showModal({
  364. title: '提示',
  365. content: msg,
  366. success(res) {
  367. if (res.confirm) {
  368. success.call(this);
  369. } else {
  370. if (cancel) cancel.call(this);
  371. }
  372. }
  373. })
  374. }
  375. const msgConfirmText = (msg, confirmText, success, cancel) => {
  376. uni.showModal({
  377. title: '提示',
  378. content: msg,
  379. confirmText: confirmText,
  380. success(res) {
  381. if (res.confirm) {
  382. success.call(this);
  383. } else {
  384. if (cancel) cancel.call(this);
  385. }
  386. }
  387. })
  388. }
  389. const alertMsg = (msg, success) => {
  390. uni.showModal({
  391. title: '提示',
  392. content: msg,
  393. showCancel: false,
  394. success(res) {
  395. if (success) success.call(this);
  396. }
  397. })
  398. return false;
  399. }
  400. const load = msg => {
  401. uni.showLoading({
  402. title: msg,
  403. mask: true
  404. })
  405. }
  406. const g = (url, success, isLoad) => {
  407. getRequest(url, {}, success, isLoad)
  408. }
  409. const p = (url, success, isLoad) => {
  410. postRequest(url, {}, success, isLoad)
  411. }
  412. const send = (url, mobile, success, error) => {
  413. if (!mobile) {
  414. return msg('手机号码不能为空', error);
  415. }
  416. postRequest(url, {
  417. phone: mobile
  418. }, json => {
  419. if (json.code !== 0) {
  420. return msg(json.msg, error);
  421. }
  422. let time = 60;
  423. const initTime = setInterval(() => {
  424. time--;
  425. if (time > 0) {
  426. success.call(this, time + '秒后获取');
  427. } else {
  428. clearInterval(initTime);
  429. success.call(this, '获取验证码');
  430. }
  431. }, 1000)
  432. })
  433. }
  434. // 登陆拦截
  435. const redirectIndex = (params) => {
  436. // uni.clearStorageSync();
  437. // console.log(params)
  438. var pages = getCurrentPages();
  439. // console.log("pages》》》》》登陆拦截", pages);
  440. let currentUrl;
  441. if (pages) {
  442. var currentPage = pages[pages.length - 1];
  443. if (currentPage) {
  444. currentUrl = currentPage.route;
  445. let query = currentPage.data.query;
  446. if (query) {
  447. for (let key in query) {
  448. const fo = key + '=' + query[key];
  449. currentUrl += currentUrl.indexOf('?') > -1 ? '&' + fo : '?' + fo;
  450. }
  451. }
  452. if (currentUrl) {
  453. console.log('currentUrl》》》》》', currentUrl);
  454. setStorage("REDIRECT_URL", '/' + currentUrl)
  455. if (currentUrl == 'pages/authorize/authorize') {
  456. console.log('当前已经处于登录页面,不在向下执行跳转');
  457. return
  458. }
  459. }
  460. }
  461. }
  462. let redirectUrl = '/pages/authorize/authorize' + (params ? params : '');
  463. // console.log("登陆拦截")
  464. uni.navigateTo({
  465. url: redirectUrl
  466. })
  467. return false;
  468. }
  469. const saveImage = (url) => {
  470. uni.saveImageToPhotosAlbum({
  471. filePath: url,
  472. success() {
  473. msg('图片保存成功');
  474. }
  475. })
  476. }
  477. const saveImageToPhotosAlbum = (url) => {
  478. if (!url) return msg('小程序码不存在');
  479. uni.getImageInfo({
  480. src: url,
  481. success(json) {
  482. // uni.getSetting({
  483. // success(res) {
  484. // if (!res.authSetting['scope.writePhotosAlbum']) {
  485. // uni.authorize({
  486. // scope: 'scope.writePhotosAlbum',
  487. // success() {
  488. // saveImage(json.path);
  489. // }
  490. // })
  491. // } else {
  492. // saveImage(json.path);
  493. // }
  494. // }
  495. // })
  496. }
  497. })
  498. }
  499. const getConfig = (config, id) => {
  500. let con = {};
  501. config.info.forEach(inf => {
  502. // console.log(inf.version, inf.version ? inf.version.indexOf(id) : '-')
  503. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  504. con[inf.setName] = inf.setDefault;
  505. });
  506. config.card.forEach(inf => {
  507. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  508. con[inf.setName] = inf.setDefault;
  509. });
  510. config.realTime.forEach(inf => {
  511. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  512. con[inf.setName] = inf.setDefault;
  513. });
  514. return con;
  515. }
  516. const isLogin = async (error) => {
  517. let userInfo = getStorage('userInfo');
  518. if (!userInfo || !userInfo.id) {
  519. if (error) return false;
  520. // console.log("第一次执行")
  521. return redirectIndex();
  522. }
  523. return true;
  524. }
  525. //检测是否登录
  526. const isLogins = (isGoLogin) => {
  527. let userInfo = getStorage('userInfo');
  528. if (!userInfo) {
  529. if (isGoLogin) {
  530. redirectIndex();
  531. }
  532. return false;
  533. }
  534. return true;
  535. }
  536. const setStorage = (key, value) => {
  537. uni.setStorageSync(env.NODE_ENV + "_" + key, value)
  538. }
  539. const getStorage = (key) => {
  540. return uni.getStorageSync(env.NODE_ENV + "_" + key)
  541. }
  542. const removeStorage = (key) => {
  543. return uni.removeStorageSync(env.NODE_ENV + "_" + key);
  544. }
  545. /**
  546. * 缓存模板id
  547. */
  548. // const gettmplIds = () => {
  549. // getRequest('/api/wxmes/querylist', {}, res => {
  550. // setStorage('tmplIds', res)
  551. // console.log(res)
  552. // })
  553. // }
  554. //微信支付订单公共方法
  555. const payOrder = (id, success) => {
  556. var loctionAddressMap = getStorage('loctionAddressMap');
  557. var datas = {
  558. id: id
  559. };
  560. if (loctionAddressMap) {
  561. datas.province = loctionAddressMap.province;
  562. datas.city = loctionAddressMap.city;
  563. };
  564. if (getStorage('isVideoScene')) {
  565. uni.checkBeforeAddOrder({
  566. success(res) {
  567. console.log('下单前置检查', res)
  568. let data = res.data;
  569. data.requiredFundType = 0;
  570. datas.sceneCheckStr = JSON.stringify(data);
  571. wexinPay(datas, success)
  572. },
  573. })
  574. } else {
  575. wexinPay(datas, success)
  576. }
  577. }
  578. //支付中或已支付
  579. const payStatus = (json, id, success) => {
  580. if (json && json.code === 115) {
  581. setTimeout(res => {
  582. uni.hideLoading();
  583. isShowLoading = false;
  584. uni.showModal({
  585. title: '提示',
  586. content: '当前订单正在支付或30秒后重试',
  587. confirmText: '我知道了',
  588. showCancel: false,
  589. success(res) {}
  590. })
  591. }, 3000)
  592. return false;
  593. }
  594. if (json.type === 2 || json.type === 3) {
  595. //调用后台判断订单是否支付成功
  596. let i = 0;
  597. let timer = setInterval(() => {
  598. postRequest('/api/order/check', {
  599. id: id
  600. }, res => {
  601. if (res.isSuccess || i === 4) {
  602. clearInterval(timer);
  603. success.call(this, res.isSuccess);
  604. } else {
  605. i++;
  606. }
  607. }, true)
  608. }, 200);
  609. // success.call(this);
  610. return false;
  611. }
  612. }
  613. // 支付成功回调
  614. const paySuccess = (id, success) => {
  615. console.log('支付成功')
  616. let i = 0;
  617. let timer = setInterval(() => {
  618. postRequest('/api/order/check', {
  619. id: id
  620. }, res => {
  621. console.log(i)
  622. if (res.isSuccess || i === 5) {
  623. clearInterval(timer);
  624. success.call(this, res.isSuccess);
  625. } else {
  626. i++;
  627. }
  628. }, true)
  629. }, 500);
  630. }
  631. // 支付失败回调
  632. const payFail = (id) => {
  633. console.log('支付失败')
  634. postRequest('/api/order/cancelPay', {
  635. id: id
  636. }, data => {
  637. uni.redirectTo({
  638. url: '/mine/orderDet/orderDet?id=' + id,
  639. })
  640. })
  641. }
  642. const wexinPay = (datas, success) => {
  643. let isShowLoading = false;
  644. if (!isShowLoading) {
  645. loadIng('加载中');
  646. // console.log('加载中')
  647. isShowLoading = true;
  648. }
  649. // #ifdef MP-WEIXIN
  650. postRequest('/api/order/weixin/pay', datas, json => {
  651. // console.log('json',json)
  652. payStatus(json, datas.id, success)
  653. const sceneCheckStr = datas.sceneCheckStr;
  654. if (sceneCheckStr && JSON.parse(sceneCheckStr).requiredFundType === 1) {
  655. console.log('拉起收银台')
  656. wx.requestOrderPayment({
  657. timeStamp: json.timeStamp,
  658. nonceStr: json.nonceStr,
  659. package: json.packages,
  660. signType: json.signType,
  661. orderInfo: json.orderInfo,
  662. paySign: json.sign,
  663. success: function() {
  664. paySuccess(datas.id, success)
  665. },
  666. fail: function(res) {
  667. console.log(res);
  668. payFail(datas.id)
  669. }
  670. })
  671. } else {
  672. uni.requestPayment({
  673. timeStamp: json.timeStamp,
  674. nonceStr: json.nonceStr,
  675. package: json.packages,
  676. signType: json.signType,
  677. paySign: json.sign,
  678. success: function() {
  679. paySuccess(datas.id, success)
  680. },
  681. fail: function(res) {
  682. console.log(res);
  683. payFail(datas.id)
  684. }
  685. })
  686. }
  687. if (isShowLoading) {
  688. uni.hideLoading();
  689. isShowLoading = false;
  690. }
  691. })
  692. // #endif
  693. // #ifdef MP-ALIPAY
  694. datas.payChannel = 'alipay_lite ';
  695. postRequest('/api/order/weixin/pay', datas, json => {
  696. // console.log(json)
  697. if (json.type === 2 || json.type === 3) {
  698. //调用后台判断订单是否支付成功
  699. let i = 0;
  700. let timer = setInterval(() => {
  701. postRequest('/api/order/check', {
  702. id: id
  703. }, res => {
  704. if (res.isSuccess || i === 4) {
  705. clearInterval(timer);
  706. success.call(this, res.isSuccess);
  707. } else {
  708. i++;
  709. }
  710. }, true)
  711. }, 200);
  712. // success.call(this);
  713. return false;
  714. }
  715. uni.requestPayment({
  716. provider: 'alipay',
  717. orderInfo: json.packages,
  718. success: function() {
  719. paySuccess(datas.id, success)
  720. },
  721. fail: function(res) {
  722. payFail(datas.id)
  723. console.log(res);
  724. }
  725. })
  726. if (isShowLoading) {
  727. uni.hideLoading();
  728. isShowLoading = false;
  729. }
  730. })
  731. // #endif
  732. }
  733. //微信支付订单公共方法
  734. const payOrdersss = (id, success) => {
  735. let isShowLoading = false;
  736. if (!isShowLoading) {
  737. loadIng('加载中');
  738. // console.log('加载中')
  739. isShowLoading = true;
  740. }
  741. var loctionAddressMap = getStorage('loctionAddressMap');
  742. var datas = {
  743. id: id
  744. };
  745. if (loctionAddressMap) {
  746. datas.province = loctionAddressMap.province;
  747. datas.city = loctionAddressMap.city;
  748. };
  749. // #ifdef MP-WEIXIN
  750. postRequest('/api/order/weixin/pay', datas, json => {
  751. // console.log(json)
  752. if (json.type === 2 || json.type === 3) {
  753. //调用后台判断订单是否支付成功
  754. let i = 0;
  755. let timer = setInterval(() => {
  756. postRequest('/api/order/check', {
  757. id: id
  758. }, res => {
  759. if (res.isSuccess || i === 4) {
  760. clearInterval(timer);
  761. success.call(this, res.isSuccess);
  762. } else {
  763. i++;
  764. }
  765. }, true)
  766. }, 200);
  767. // success.call(this);
  768. return false;
  769. }
  770. uni.requestPayment({
  771. timeStamp: json.timeStamp,
  772. nonceStr: json.nonceStr,
  773. package: json.packages,
  774. signType: json.signType,
  775. paySign: json.sign,
  776. success: function() {
  777. console.log('支付成功')
  778. let i = 0;
  779. let timer = setInterval(() => {
  780. postRequest('/api/order/check', {
  781. id: id
  782. }, res => {
  783. console.log(i)
  784. if (res.isSuccess || i === 5) {
  785. clearInterval(timer);
  786. success.call(this, res.isSuccess);
  787. } else {
  788. i++;
  789. }
  790. }, true)
  791. }, 500);
  792. },
  793. fail: function(res) {
  794. console.log('支付失败')
  795. postRequest('/api/order/cancelPay', {
  796. id: id
  797. }, data => {
  798. uni.redirectTo({
  799. url: '/mine/orderDet/orderDet?id=' + id,
  800. })
  801. })
  802. console.log(res);
  803. }
  804. })
  805. if (isShowLoading) {
  806. uni.hideLoading();
  807. isShowLoading = false;
  808. }
  809. })
  810. // #endif
  811. // #ifdef MP-ALIPAY
  812. datas.payChannel = 'alipay_lite ';
  813. postRequest('/api/order/weixin/pay', datas, json => {
  814. // console.log(json)
  815. if (json.type === 2 || json.type === 3) {
  816. //调用后台判断订单是否支付成功
  817. let i = 0;
  818. let timer = setInterval(() => {
  819. postRequest('/api/order/check', {
  820. id: id
  821. }, res => {
  822. if (res.isSuccess || i === 4) {
  823. clearInterval(timer);
  824. success.call(this, res.isSuccess);
  825. } else {
  826. i++;
  827. }
  828. }, true)
  829. }, 200);
  830. // success.call(this);
  831. return false;
  832. }
  833. uni.requestPayment({
  834. provider: 'alipay',
  835. orderInfo: json.packages,
  836. success: function() {
  837. console.log('支付成功')
  838. let i = 0;
  839. let timer = setInterval(() => {
  840. postRequest('/api/order/check', {
  841. id: id
  842. }, res => {
  843. console.log(i)
  844. if (res.isSuccess || i === 5) {
  845. clearInterval(timer);
  846. success.call(this, res.isSuccess);
  847. } else {
  848. i++;
  849. }
  850. }, true)
  851. }, 500);
  852. },
  853. fail: function(res) {
  854. console.log('支付失败')
  855. postRequest('/api/order/cancelPay', {
  856. id: id
  857. }, data => {
  858. uni.redirectTo({
  859. url: '/mine/orderDet/orderDet?id=' + id,
  860. })
  861. })
  862. console.log(res);
  863. }
  864. })
  865. if (isShowLoading) {
  866. uni.hideLoading();
  867. isShowLoading = false;
  868. }
  869. })
  870. // #endif
  871. }
  872. //微信支付订单公共方法 ==活动
  873. const payOrders = (id, success, type) => {
  874. let isShowLoading = false;
  875. if (!isShowLoading) {
  876. loadIng('加载中');
  877. // console.log('加载中')
  878. isShowLoading = true;
  879. }
  880. postRequest('/api/order/weixin/pay', {
  881. id: id
  882. }, json => {
  883. // console.log(json)
  884. payStatus(json, id, success)
  885. uni.requestPayment({
  886. timeStamp: json.timeStamp,
  887. nonceStr: json.nonceStr,
  888. package: json.packages,
  889. signType: json.signType,
  890. paySign: json.sign,
  891. success: function() {
  892. paySuccess(id, success)
  893. },
  894. fail: function(res) {
  895. console.log('支付失败')
  896. postRequest('/api/order/cancelPay', {
  897. id: id
  898. }, data => {
  899. uni.redirectTo({
  900. url: '/mine/activity/activity',
  901. })
  902. })
  903. console.log(res);
  904. }
  905. })
  906. if (isShowLoading) {
  907. uni.hideLoading();
  908. isShowLoading = false;
  909. }
  910. })
  911. }
  912. const authSetting = (authority, success, error) => {
  913. // console.log(authority,success)
  914. // #ifndef H5
  915. uni.getSetting({
  916. success(res) {
  917. if (res.authSetting[authority]) {
  918. success.call(this);
  919. return false;
  920. }
  921. uni.authorize({
  922. scope: authority,
  923. success() {
  924. success.call(this);
  925. },
  926. fail: function(res) {
  927. error.call(this);
  928. }
  929. })
  930. }
  931. })
  932. // #endif
  933. }
  934. const getLocation = (suss) => {
  935. // #ifdef MP-WEIXIN
  936. authSetting('scope.userLocation', () => {
  937. // load('定位中…');
  938. uni.getLocation({
  939. type: 'gcj02',
  940. // isHighAccuracy: true,
  941. success: function(res) {
  942. // uni.hideLoading();
  943. console.log(res, "789")
  944. suss.call(this, res);
  945. },
  946. fail: function(res) {
  947. console.log("调用失败", res)
  948. suss.call(this, 2)
  949. // uni.hideLoading();
  950. }
  951. })
  952. }, () => {
  953. // uni.hideLoading();
  954. console.log("取消")
  955. suss.call(this, 1)
  956. });
  957. // #endif
  958. // #ifdef MP-ALIPAY
  959. uni.getLocation({
  960. type: 'gcj02',
  961. // isHighAccuracy: true,
  962. success: function(res) {
  963. // uni.hideLoading();
  964. console.log(res, "789")
  965. suss.call(this, res);
  966. },
  967. fail: function(res) {
  968. console.log("调用失败", res)
  969. suss.call(this, 2)
  970. // uni.hideLoading();
  971. }
  972. })
  973. // #endif
  974. // #ifdef H5
  975. uni.getLocation({
  976. type: 'gcj02',
  977. isHighAccuracy: true,
  978. success: function(res) {
  979. // uni.hideLoading();
  980. console.log(res)
  981. suss.call(this, res, "789");
  982. },
  983. fail: function() {
  984. console.log("调用失败", res)
  985. suss.call(this, 2)
  986. // uni.hideLoading();
  987. }
  988. })
  989. // #endif
  990. }
  991. const scopeAddress = (success) => {
  992. // #ifdef MP-WEIXIN
  993. authSetting('scope.address', () => {
  994. uni.chooseAddress({
  995. success: function(res) {
  996. success.call(this, res);
  997. },
  998. })
  999. }, () => {
  1000. msg('未设置开放权限')
  1001. });
  1002. // #endif
  1003. // #ifdef MP-ALIPAY
  1004. uni.chooseAddress({
  1005. success: function(res) {
  1006. success.call(this, res);
  1007. },
  1008. })
  1009. // #endif
  1010. }
  1011. const isAuth = () => {
  1012. const user = getStorage('userInfo');
  1013. return user && user.id;
  1014. }
  1015. // 去掉字符串中的特殊字符和转义字符
  1016. const excludeSpecial = (s) => {
  1017. // 去掉转义字符
  1018. // s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
  1019. const pattern = /[`~!@#$^&*()=|{}':;',\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""' + - - _ % 。,、?\s]/g;
  1020. if (s != undefined || s != null) {
  1021. s = s.replace(pattern, "")
  1022. }
  1023. return s;
  1024. }
  1025. // 数字单位格式化
  1026. function unitConverter(num) {
  1027. // text 数字+单位 number格式化后的数字 unit单位
  1028. console.log(num)
  1029. if (!num || isNaN(num)) {
  1030. return {
  1031. text: 0,
  1032. number: 0,
  1033. unit: ''
  1034. }
  1035. }
  1036. num = Number(num)
  1037. if (Math.abs(num) > 100000000) {
  1038. return {
  1039. text: (num / 100000000).toFixed(2) + '亿',
  1040. number: (num / 100000000).toFixed(2),
  1041. unit: '亿'
  1042. }
  1043. } else if (Math.abs(num) > 10000) {
  1044. return {
  1045. text: (num / 10000).toFixed(2) + '万',
  1046. number: (num / 10000).toFixed(2),
  1047. unit: '万'
  1048. }
  1049. } else {
  1050. return {
  1051. text: num,
  1052. number: num,
  1053. unit: ''
  1054. }
  1055. }
  1056. }
  1057. // 行为操作
  1058. const saveBehavior = (params, success) => {
  1059. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  1060. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  1061. postRequest('/api/v3/behavior/save', params, data => {
  1062. success.call(this, data)
  1063. })
  1064. }
  1065. const getBehavior = (params, success) => {
  1066. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  1067. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  1068. console.log('行为')
  1069. getRequest('/api/v3/behavior/get', params, data => {
  1070. success.call(this, data)
  1071. })
  1072. }
  1073. /**
  1074. * 跳入聊天
  1075. * @param {Object} userId 对方用户ID
  1076. * @param {Object} avatar 对方用户头像
  1077. * @param {Object} realName 对方用户名称
  1078. */
  1079. const jumpSingleChatRoom = function(userId, avatar, realName) {
  1080. var my = uni.getStorageSync("myUsername");
  1081. var nameList = {
  1082. myName: my,
  1083. your: 'rs_' + userId
  1084. };
  1085. uni.setStorage({
  1086. key: 'imUserInfo_' + nameList.your,
  1087. data: {
  1088. avatar: avatar,
  1089. id: userId,
  1090. nickName: realName
  1091. }
  1092. });
  1093. uni.navigateTo({
  1094. url: "/chat/chatroom/chatroom?username=" + JSON.stringify(nameList)
  1095. });
  1096. }
  1097. /**
  1098. * text:展示导航名
  1099. * iconPath:未点击显示图标
  1100. * selectedIconPath:选中图标
  1101. * pagePath:页面路径
  1102. * channel:匹配名
  1103. * click:点击事件
  1104. * index:排序
  1105. * show:是否显示
  1106. */
  1107. var selectedIconPath
  1108. if (header.appId == 'ZQ1VK5oc17I387E') {
  1109. selectedIconPath = '/static/pages/images/' + publics.iconSrc + 'yunyao_h.png'
  1110. } else {
  1111. selectedIconPath = '/static/pages/images/' + publics.iconSrc + 'home_h.png'
  1112. }
  1113. var tab = [{
  1114. text: "首页",
  1115. iconPath: "/static/pages/images/home.png",
  1116. selectedIconPath: '/static/pages/images/home_h.png',
  1117. pagePath: 'pages/index/index',
  1118. channel: 'home',
  1119. click: 'goHome',
  1120. index: 1,
  1121. show: true
  1122. },
  1123. {
  1124. text: "活动",
  1125. iconPath: "/static/pages/images/activity.png",
  1126. selectedIconPath: '/static/pages/images/activity_h.png',
  1127. pagePath: 'pages/matchList/index',
  1128. click: 'goMatch',
  1129. channel: 'match',
  1130. index: 2,
  1131. show: true
  1132. },
  1133. {
  1134. text: "社区",
  1135. iconPath: "/static/pages/images/interactive.png",
  1136. selectedIconPath: '/static/pages/images/interactive_h.png',
  1137. pagePath: 'pages/interactive/index',
  1138. click: 'goInteractive',
  1139. channel: 'interactive',
  1140. index: 3,
  1141. show: true
  1142. },
  1143. {
  1144. text: "发现",
  1145. iconPath: "/static/pages/images/found.png",
  1146. selectedIconPath: '/static/pages/images/found_h.png',
  1147. pagePath: 'pages/found/found',
  1148. click: 'goFound',
  1149. channel: 'found',
  1150. index: 3,
  1151. show: true
  1152. },
  1153. {
  1154. text: "我的",
  1155. iconPath: '/static/pages/images/user.png',
  1156. selectedIconPath: '/static/pages/images/user_h.png',
  1157. pagePath: 'pages/user/user',
  1158. click: 'goUser',
  1159. channel: 'user',
  1160. index: 6,
  1161. show: true
  1162. }
  1163. ]
  1164. module.exports = {
  1165. setStorage: setStorage,
  1166. getStorage: getStorage,
  1167. postRequest: postRequest,
  1168. postRequestLoding: postRequestLoding,
  1169. getRequest: getRequest,
  1170. putRequest: putRequest,
  1171. putRequestJson: putRequestJson,
  1172. msg: msg,
  1173. g: g,
  1174. p: p,
  1175. env: env,
  1176. send: send,
  1177. redirectIndex: redirectIndex,
  1178. saveImage: saveImage,
  1179. saveImageToPhotosAlbum: saveImageToPhotosAlbum,
  1180. uploadFile: uploadFile,
  1181. msgConfirm: msgConfirm,
  1182. getConfig: getConfig,
  1183. load: load,
  1184. isLogin: isLogin,
  1185. isLogins: isLogins,
  1186. payOrder: payOrder,
  1187. payOrders: payOrders,
  1188. getLocation: getLocation,
  1189. scopeAddress: scopeAddress,
  1190. isAuth: isAuth,
  1191. alertMsg: alertMsg,
  1192. getToken: getToken,
  1193. loadIng: loadIng,
  1194. removeStorage: removeStorage,
  1195. public: publics,
  1196. header: header,
  1197. // gettmplIds: gettmplIds,
  1198. excludeSpecial: excludeSpecial,
  1199. tab: tab,
  1200. msgConfirmText: msgConfirmText,
  1201. silenceLogin: silenceLogin,
  1202. getWXUserProfile: getWXUserProfile,
  1203. updataWxInfos: updataWxInfos,
  1204. saveBehavior: saveBehavior,
  1205. getBehavior: getBehavior,
  1206. unitConverter: unitConverter,
  1207. initIMChat: initIMChat,
  1208. getIMToken: getIMToken,
  1209. jumpConversation: jumpConversation,
  1210. jumpSingleChatRoom: jumpSingleChatRoom
  1211. }