金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
6.5 KiB

  1. <template>
  2. <view id="free">
  3. <view class="free-title">
  4. <image mode="widthFix" src="https://ibrand-miniprogram.oss-cn-hangzhou.aliyuncs.com/%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%88%86%E7%BB%84(1).png">
  5. </image>
  6. </view>
  7. <view class="free-content">
  8. <view class="inputs-box">
  9. <view class="item mx-1px-bottom first-input">
  10. <span>会员名</span>
  11. <input type="text" disabled="true" :value="user.nick_name"></input>
  12. </view>
  13. <view class="item mx-1px-bottom second-input">
  14. <span>手机号</span>
  15. <input type="text" disabled="true" :value="user.mobile"></input>
  16. </view>
  17. <view class="item second-input">
  18. <span>免单金额</span>
  19. <input type="text" placeholder="请询问服务员后输入" placeholder-class="input-placeholder" @input="changeAmount" :value="amount"></input>
  20. </view>
  21. </view>
  22. <view class="pay-btn" @tap="soonPay">立即支付</view>
  23. <view class="pay-attention">*会员充值概不退款</view>
  24. </view>
  25. <view class="free-foot">
  26. <image mode="widthFix" src="https://ibrand-miniprogram.oss-cn-hangzhou.aliyuncs.com/%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%88%86%E7%BB%8401.png"></image>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
  32. export default {
  33. data() {
  34. return {
  35. user: '',
  36. amount: ''
  37. };
  38. },
  39. onLoad(e) {
  40. pageLogin(getUrl(), token => {
  41. this.queryUserInfo(token);
  42. });
  43. },
  44. components: {},
  45. props: {},
  46. methods: {
  47. changeAmount(e) {
  48. var money = e.detail.value;
  49. if (!money) {
  50. money = '';
  51. } else if (/\S*$/.test(money)) {
  52. money = money.replace(/[^\d\.]|^\./g, '').replace(/\.{2}/g, '.').replace(/^([1-9]\d*|0)(\.\d{1,2})(\.|\d{1})?$/, '$1$2').replace(/^0\d{1}/g, '0');
  53. }
  54. this.setData({
  55. amount: money
  56. });
  57. },
  58. queryUserInfo(token) {
  59. this.$http.get({
  60. api: 'api/me',
  61. header: {
  62. Authorization: token
  63. }
  64. }).then(res => {
  65. if (res.statusCode == 200) {
  66. this.setData({
  67. user: res.data.data
  68. });
  69. } else {
  70. wx.showModal({
  71. title: '提示',
  72. content: '数据请求失败',
  73. success: res => {}
  74. });
  75. }
  76. });
  77. },
  78. //获取openid
  79. getOpenid() {
  80. return new Promise((resolve, reject) => {
  81. wx.login({
  82. success: res => {
  83. this.$http.post({
  84. api: `api/oauth/miniprogram/openid`,
  85. data: {
  86. code: res.code
  87. }
  88. }).then(res => {
  89. res = res.data;
  90. resolve(res.data.openid);
  91. }).catch(() => {
  92. reject('获取openid失败');
  93. });
  94. },
  95. fail: () => {
  96. wx.showModal({
  97. content: "接口请求失败"
  98. });
  99. }
  100. });
  101. });
  102. },
  103. soonPay() {
  104. var message = '';
  105. if (this.amount == '') {
  106. message = '请您输入免单金额';
  107. wx.showModal({
  108. content: message,
  109. showCancel: false
  110. });
  111. } else {
  112. //请求接口
  113. this.submitPay();
  114. }
  115. },
  116. //请求免单支付接口
  117. submitPay() {
  118. wx.showLoading({
  119. title: '加载中'
  120. });
  121. this.getOpenid().then(res => {
  122. var token = this.$cookieStorage.get('user_token');
  123. var data = {
  124. amount: this.amount * 100,
  125. channel: 'wx_lite',
  126. openid: res
  127. };
  128. this.$http.post({
  129. api: `api/users/balance/charge/amount`,
  130. data: data,
  131. header: {
  132. Authorization: token
  133. }
  134. }).then(res => {
  135. res = res.data;
  136. if (res.status) {
  137. this.newcharge(true, res.data.charge);
  138. } else {
  139. this.newcharge(false, res.message);
  140. }
  141. }).catch(rej => {
  142. wx.showModal({
  143. content: '请求支付失败,请重试!'
  144. });
  145. });
  146. }).catch(() => {
  147. wx.hideLoading();
  148. wx.showModal({
  149. content: '支付失败',
  150. showCancel: false
  151. });
  152. });
  153. },
  154. // 新版支付
  155. newcharge(success, charge) {
  156. if (success) {
  157. var that = this;
  158. if (charge.pay_scene == 'test') {
  159. wx.showModal({
  160. content: '不支持模拟支付',
  161. showCancel: false
  162. });
  163. wx.hideLoading();
  164. } else {
  165. wx.requestPayment({
  166. "timeStamp": charge.wechat.timeStamp,
  167. "nonceStr": charge.wechat.nonceStr,
  168. "package": charge.wechat.package,
  169. "signType": charge.wechat.signType,
  170. "paySign": charge.wechat.paySign,
  171. success: res => {
  172. if (res.errMsg == 'requestPayment:ok') {
  173. wx.hideLoading();
  174. wx.redirectTo({
  175. url: `/pages/recharge/success/success?order_no=${charge.order_no}`
  176. });
  177. } else {
  178. wx.showModal({
  179. content: '调用支付失败!',
  180. showCancel: false
  181. });
  182. }
  183. },
  184. fail: err => {
  185. wx.hideLoading();
  186. if (err.errMsg == 'requestPayment:fail cancel') {
  187. wx.switchTab({
  188. url: `/pages/user/personal/personal`
  189. });
  190. } else {
  191. wx.showModal({
  192. content: '调用支付失败!',
  193. showCancel: false
  194. });
  195. }
  196. }
  197. });
  198. }
  199. } else {
  200. wx.hideLoading();
  201. wx.showModal({
  202. content: charge || '请求支付失败,请重试!',
  203. showCancel: false
  204. });
  205. }
  206. },
  207. setData: function (obj) {
  208. let that = this;
  209. let keys = [];
  210. let val, data;
  211. Object.keys(obj).forEach(function (key) {
  212. keys = key.split('.');
  213. val = obj[key];
  214. data = that.$data;
  215. keys.forEach(function (key2, index) {
  216. if (index + 1 == keys.length) {
  217. that.$set(data, key2, val);
  218. } else {
  219. if (!data[key2]) {
  220. that.$set(data, key2, {});
  221. }
  222. }
  223. data = data[key2];
  224. });
  225. });
  226. }
  227. },
  228. computed: {},
  229. watch: {}
  230. };
  231. </script>
  232. <style rel="stylesheet/less" lang="less">
  233. @import "free";
  234. </style>