request.js 29 KB

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