User.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\DeliveryTrade;
  4. use app\api\model\Detail;
  5. use app\api\model\Goods;
  6. use app\api\model\MoneyRecord;
  7. use app\api\model\CoinRecord;
  8. use app\api\model\Delivery;
  9. use app\api\model\Order;
  10. use app\api\model\Zz;
  11. use app\api\model\Prizerecord;
  12. use app\api\model\SearchHistory;
  13. use app\api\model\Setting;
  14. use app\api\model\Star;
  15. use app\api\model\Text;
  16. use app\api\model\UserAddress;
  17. use app\api\model\Version;
  18. use app\api\model\Withdrawal;
  19. use app\common\controller\Api;
  20. use app\common\library\Sms;
  21. use Endroid\QrCode\ErrorCorrectionLevel;
  22. use Endroid\QrCode\QrCode;
  23. use fast\Random;
  24. use think\Db;
  25. use think\Exception;
  26. use think\Validate;
  27. /**
  28. * 会员接口
  29. */
  30. class User extends Api
  31. {
  32. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third','userCapital','lunbobox'];
  33. protected $noNeedRight = '*';
  34. protected $lockUserId = ['applyDelivery', 'exchange', 'moneyToCoin', 'withdrawal'];
  35. public function _initialize()
  36. {
  37. parent::_initialize();
  38. }
  39. /**
  40. * 手机验证码登录
  41. *
  42. * @param string $mobile 手机号
  43. * @param string $captcha 验证码
  44. */
  45. public function mobilelogin()
  46. {
  47. $mobile = input('mobile');
  48. $captcha = input('captcha');
  49. $invite_code = input('sharecode', '');
  50. $is_channel = input('is_channel', '');
  51. //print_r($invite_code);//是否特定渠道进来
  52. $is_notice = 0; //是否弹窗
  53. if (!$mobile || !$captcha) {
  54. $this->error('参数错误');
  55. }
  56. if (!Validate::regex($mobile, "^1\d{10}$")) {
  57. $this->error('手机号格式不正确');
  58. }
  59. if (!Sms::check($mobile, $captcha, 'login') && !Sms::check($mobile, $captcha, 'register')) {
  60. $this->error('验证码不正确');
  61. }
  62. $user = \app\common\model\User::getByMobile($mobile);
  63. if ($user) {
  64. if ($user->status != 'normal') {
  65. $this->error('账号被锁定');
  66. }
  67. //如果已经有账号则直接登录
  68. $ret = $this->auth->direct($user->id);
  69. } else {
  70. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, ['invite_code' => $invite_code]);
  71. if($ret && $is_channel == '49ba59abbe56e057'){ //赠送10金币
  72. \app\common\model\User::where(['id'=>$this->auth->id])->setInc("coin", 10);
  73. $is_notice = 1;
  74. }
  75. }
  76. if ($ret) {
  77. Sms::flush($mobile, 'login');
  78. $data = $this->auth->getUserinfo();
  79. $data['money'] = $data['money'] ? floatval($data['money']) : 0;
  80. $data['coin'] = $data['coin'] ? floatval($data['coin']) : 0;
  81. $data['avatar'] = $data['avatar'] ? cdnurl($data['avatar'], true) : '';
  82. $data['is_notice'] = $is_notice;
  83. unset($data['id']);
  84. unset($data['user_id']);
  85. unset($data['createtime']);
  86. unset($data['expiretime']);
  87. unset($data['expires_in']);
  88. $registerurl = db('setting')->where('id', 1)->find();
  89. if($registerurl['registerurl'] == NULL){
  90. $data['un'] = 1;
  91. $this->success('注册成功',$data);
  92. }else{
  93. $data['registerurl'] = $registerurl['registerurl'];
  94. $data['un'] = 0;
  95. $this->success('注册成功',$data);
  96. }
  97. $this->success('登录成功', $data);
  98. } else {
  99. $this->error($this->auth->getError());
  100. }
  101. }
  102. /**
  103. * 账号密码登录
  104. */
  105. public function login()
  106. {
  107. $account = input('mobile');
  108. $password = input('password');
  109. if (!$account || !$password) {
  110. $this->error('账号和密码不能为空');
  111. }
  112. $ret = $this->auth->login($account, $password);
  113. if ($ret) {
  114. $data = $this->auth->getUserinfo();
  115. $data['money'] = $data['money'] ? floatval($data['money']) : 0;
  116. $data['avatar'] = $data['avatar'] ? cdnurl($data['avatar'], true) : '';
  117. unset($data['id']);
  118. unset($data['user_id']);
  119. unset($data['createtime']);
  120. unset($data['expiretime']);
  121. unset($data['expires_in']);
  122. $this->success('登录成功', $data);
  123. } else {
  124. $this->error($this->auth->getError());
  125. }
  126. }
  127. /**
  128. * 注册会员
  129. */
  130. public function register()
  131. {
  132. $mobile = input('mobile');
  133. $password = input('password');
  134. $captcha = input('captcha');
  135. $invite_code = input('sharecode', '');
  136. if (!$mobile || !$password) {
  137. $this->error('参数错误');
  138. }
  139. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  140. $this->error('手机号格式不正确');
  141. }
  142. $ret = Sms::check($mobile, $captcha, 'register');
  143. // if (!$ret) {
  144. // $this->error('验证码不正确');
  145. // }
  146. $ret = $this->auth->register($mobile, $password, '', $mobile, ['invite_code' => $invite_code]);
  147. if ($ret) {
  148. $registerurl = db('setting')->where('id', 1)->find();
  149. Sms::flush($mobile, 'register');
  150. $data = $this->auth->getUserinfo();
  151. $data['money'] = $data['money'] ? floatval($data['money']) : 0;
  152. $data['avatar'] = $data['avatar'] ? cdnurl($data['avatar'], true) : '';
  153. unset($data['id']);
  154. unset($data['user_id']);
  155. unset($data['createtime']);
  156. unset($data['expiretime']);
  157. unset($data['expires_in']);
  158. if($registerurl['registerurl'] == NULL){
  159. $data['un'] = 1;
  160. $this->success('注册成功',$data);
  161. }else{
  162. $data['registerurl'] = $registerurl['registerurl'];
  163. $data['un'] = 0;
  164. $this->success('注册成功',$data);
  165. }
  166. } else {
  167. $this->error($this->auth->getError());
  168. }
  169. }
  170. /**
  171. * 重置密码(忘记密码)
  172. */
  173. public function resetpwd()
  174. {
  175. $mobile = input("mobile");
  176. $newpassword = input("newpassword");
  177. $captcha = input("captcha");
  178. if (!$newpassword || !$captcha) {
  179. $this->error('参数错误');
  180. }
  181. if (!Validate::regex($mobile, "^1\d{10}$")) {
  182. $this->error('手机号格式不正确');
  183. }
  184. if (!Validate::checkRule($newpassword, "min:8|max:12")) {
  185. $this->error('请设置8-12位密码');
  186. }
  187. $user = \app\common\model\User::getByMobile($mobile);
  188. if (!$user) {
  189. $this->error('用户不存在');
  190. }
  191. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  192. if (!$ret) {
  193. $this->error('验证码不正确');
  194. }
  195. Sms::flush($mobile, 'resetpwd');
  196. //模拟一次登录
  197. $this->auth->direct($user->id);
  198. $ret = $this->auth->changepwd($newpassword, '', true);
  199. if ($ret) {
  200. $this->success('重置密码成功');
  201. } else {
  202. $this->error($this->auth->getError());
  203. }
  204. }
  205. /**
  206. * 修改密码
  207. */
  208. public function changepwd()
  209. {
  210. $oldpassword = input("oldpassword");
  211. $newpassword = input("newpassword");
  212. if (!$newpassword || !$oldpassword) {
  213. $this->error('参数错误');
  214. }
  215. if (!Validate::checkRule($newpassword, "min:8|max:12")) {
  216. $this->error('请设置8-12位密码');
  217. }
  218. $ret = $this->auth->changepwd($newpassword, $oldpassword, false);
  219. if ($ret) {
  220. $this->success('修改密码成功');
  221. } else {
  222. $this->error($this->auth->getError());
  223. }
  224. }
  225. /**
  226. * 退出登录
  227. */
  228. public function logout()
  229. {
  230. $this->auth->logout();
  231. $this->success('退出成功');
  232. }
  233. /**
  234. * 修改手机号
  235. *
  236. * @param string $mobile 手机号
  237. * @param string $captcha 验证码
  238. */
  239. public function changemobile()
  240. {
  241. $user = $this->auth->getUser();
  242. $mobile = input('mobile');
  243. $captcha = input('captcha');
  244. if (!$mobile || !$captcha) {
  245. $this->error('参数错误');
  246. }
  247. if (!Validate::regex($mobile, "^1\d{10}$")) {
  248. $this->error('手机号格式不正确');
  249. }
  250. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  251. $this->error('手机号已存在');
  252. }
  253. $result = Sms::check($mobile, $captcha, 'changemobile');
  254. if (!$result) {
  255. $this->error('验证码不正确');
  256. }
  257. $verification = $user->verification;
  258. $verification->mobile = 1;
  259. $user->verification = $verification;
  260. $user->mobile = $mobile;
  261. $user->save();
  262. Sms::flush($mobile, 'changemobile');
  263. $this->success('修改成功');
  264. }
  265. /**
  266. * 绑定手机号
  267. *
  268. * @param string $mobile 手机号
  269. * @param string $captcha 验证码
  270. */
  271. public function bindMobile()
  272. {
  273. $user = $this->auth->getUser();
  274. $mobile = input('mobile');
  275. $captcha = input('captcha');
  276. if (!$mobile || !$captcha) {
  277. $this->error('参数错误');
  278. }
  279. if (!Validate::regex($mobile, "^1\d{10}$")) {
  280. $this->error('手机号格式不正确');
  281. }
  282. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  283. $this->error('手机号已存在');
  284. }
  285. $result = Sms::check($mobile, $captcha, 'bindmobile');
  286. if (!$result) {
  287. $this->error('验证码不正确');
  288. }
  289. $verification = $user->verification;
  290. $verification->mobile = 1;
  291. $user->verification = $verification;
  292. $user->mobile = $mobile;
  293. $user->save();
  294. Sms::flush($mobile, 'bindmobile');
  295. $this->success('绑定成功');
  296. }
  297. // 用户金币 和 资产
  298. public function userCapital(){
  299. if($this->auth->id){
  300. $box = db('prize_record')->where(['user_id' => $this->auth->id,'status' => 'bag'])->count();
  301. $ret = ['coin' => $this->auth->coin,'box' => $box];
  302. }else{
  303. $ret = ['coin' => 0,'box' => 0];
  304. }
  305. $this->success('查询成功', $ret);
  306. }
  307. /**
  308. * 用户信息
  309. * @author fuyelk <fuyelk@fuyelk.com>
  310. * @date 2021/07/08 15:54
  311. */
  312. public function userinfo()
  313. {
  314. $openurl = db('setting')->where('id', 1)->find();
  315. // print_r($openurl['openurl'] . '?sharecode=' . $this->auth->sharecode);die;
  316. $qrcode = new QrCode($openurl['openurl'] . '?sharecode=' . $this->auth->sharecode);
  317. // $qrcode = new QrCode($this->request->domain() . '/h5/#/?sharecode=' . $this->auth->sharecode);
  318. $avatar = db('user')->where('id', $this->auth->id)->value('avatar');
  319. if ($avatar) {
  320. $qrcode->setLogoPath(cdnurl($avatar, true));
  321. $qrcode->setLogoSize(80);
  322. }
  323. $qrcode->setErrorCorrectionLevel(ErrorCorrectionLevel::QUARTILE());
  324. $qrcode_image = $qrcode->writeDataUri();
  325. $ret = [
  326. 'nickname' => $this->auth->nickname,
  327. 'mobile' => $this->auth->mobile,
  328. 'settled_item' => $this->auth->retail_balance,
  329. 'avatar' => $this->auth->avatar ? cdnurl($this->auth->avatar, true) : '',
  330. 'money' => floatval($this->auth->money),
  331. 'coin' => $this->auth->coin,
  332. 'sharecode' => $this->auth->sharecode,
  333. 'qr_code' => $qrcode_image
  334. ];
  335. $this->success('查询成功', $ret);
  336. }
  337. /**
  338. * 我的盒柜
  339. * @throws \think\exception\DbException
  340. * @author fuyelk <fuyelk@fuyelk.com>
  341. * @date 2021/07/12 00:29
  342. */
  343. public function myBox()
  344. {
  345. $status = input('status/d');
  346. $pagesize = input('pagesize/d', 10);
  347. $page = input('page/d', 1);
  348. $statusList = [1 => 'bag', 2 => 'exchange'];
  349. if (!isset($statusList[$status])) {
  350. $this->error('状态有误');
  351. }
  352. $status = $statusList[$status];
  353. $order = 'prize.id desc';
  354. if ('exchange' == $status) {
  355. $order = 'exchange_time desc';
  356. }
  357. $list = Prizerecord::alias('prize')
  358. ->field('prize.id record_id,prize.goods_name,prize.goods_image,prize.create_time,prize.exchange_time,prize.goods_coin_price,prize.goods_rmb_price')
  359. ->field('order.coin_price box_coin_price,order.rmb_price box_rmb_price,order.pay_method')
  360. ->join('order order', 'order.id = prize.order_id')
  361. ->where('prize.user_id', $this->auth->id)
  362. ->where('prize.status', $status) // 奖品状态:bag=盒柜,exchange=已回收,delivery=申请发货,received=已收货
  363. ->order($order)
  364. ->paginate($pagesize, false, ['page' => $page])
  365. ->each(function ($item) use ($status) {
  366. $registerurl = db('setting')->where('id', 1)->find();
  367. $recovery_discount = floatval($registerurl['recovery_discount']);
  368. $item->goods_image = $item->goods_image ? cdnurl($item->goods_image, true) : '';
  369. $item->box_coin_price = intval($item->box_coin_price);
  370. $item->box_rmb_price = floatval($item->box_rmb_price);
  371. $item->goods_coin_price = intval($item->goods_coin_price);
  372. $item->goods_rmb_price = floatval($item->goods_rmb_price);
  373. $item->goods_hui_price = floor($item->goods_coin_price * round($recovery_discount / 100, 2));
  374. if ('exchange' == $status) {
  375. $item->time = date('Y-m-d H:i:s', $item->exchange_time);
  376. } else {
  377. $item->time = date('Y-m-d H:i:s', $item->create_time);
  378. }
  379. $item->hidden(['create_time', 'exchange_time']);
  380. });
  381. $this->success('查询成功', $list);
  382. }
  383. /**
  384. * 开箱记录
  385. * @throws \think\exception\DbException
  386. * @author fuyelk <fuyelk@fuyelk.com>
  387. */
  388. public function openRecord()
  389. {
  390. $pagesize = input('pagesize/d', 10);
  391. $page = input('page/d', 1);
  392. $list = Order::field('box_name,image,num,coin_amount,pay_time')
  393. ->where('status', 'used') // 状态:unpay=待支付,unused=待抽奖,used=已使用
  394. ->where('user_id', $this->auth->id)
  395. ->paginate($pagesize, false, ['page' => $page])
  396. ->each(function ($item) {
  397. $item->box_name = $item->box_name . ' x' . $item->num;
  398. $item->image = $item->image ? cdnurl($item->image, true) : '';
  399. $item->time = date('Y-m-d H:i:s', $item->pay_time);
  400. $item->hidden(['pay_time', 'num']);
  401. });
  402. $this->success('查询成功', $list);
  403. }
  404. /**
  405. * 我的订单列表
  406. * @throws \think\exception\DbException
  407. * @author fuyelk <fuyelk@fuyelk.com>
  408. */
  409. public function myOrderList()
  410. {
  411. $status = input('status/d');
  412. $pagesize = input('pagesize/d', 10);
  413. $page = input('page/d', 1);
  414. $statusList = [1 => 'undelivered', 2 => 'unreceived', 3 => 'finished'];
  415. $statusListText = ['undelivered' => '待发货', 'unreceived' => '已发货', 'finished' => '已完成'];
  416. if (!isset($statusList[$status])) {
  417. $this->error('状态有误');
  418. }
  419. $status = $statusList[$status];
  420. $list = Delivery::alias('delivery')
  421. ->field('delivery.id delivery_order_id,delivery.delivery_order_no,delivery.goods_name,delivery.goods_image,delivery.create_time,delivery.delivery_number,delivery.post_name')
  422. ->field('prize.goods_coin_price')
  423. ->join('prize_record prize', 'prize.id = delivery.prize_id')
  424. ->where('delivery.user_id', $this->auth->id)
  425. ->where('delivery.status', 'neq', 'unpay')
  426. ->where('delivery.status', $status)
  427. ->order('delivery.delivery_time desc,delivery.id desc')
  428. ->paginate($pagesize, false, ['page' => $page])
  429. ->each(function ($item) use ($status, $statusListText) {
  430. $item->status = $statusListText[$status];
  431. $item->goods_image = $item->goods_image ? cdnurl($item->goods_image, true) : '';
  432. $item->time = date('Y-m-d H:i:s', $item->create_time);
  433. $item->hidden(['create_time']);
  434. $item->delivery_number = $item->delivery_number ?: '';
  435. $item->post_name = $item->post_name ?: '';
  436. });
  437. $this->success('查询成功', $list);
  438. }
  439. /**
  440. * 发货订单详情
  441. * @author fuyelk <fuyelk@fuyelk.com>
  442. */
  443. public function deliveryOrderDetail()
  444. {
  445. $delivery_order_id = input('delivery_order_id');
  446. if (empty($delivery_order_id)) {
  447. $this->error('未选择订单');
  448. }
  449. $order = Delivery::alias('delivery')
  450. ->field('delivery.id delivery_order_id,delivery.delivery_order_no,delivery.username,delivery.mobile,
  451. delivery.province,delivery.city,delivery.area,delivery.detail,delivery.address,
  452. delivery.goods_name,delivery.goods_image,delivery.create_time delivery_apply_time,
  453. delivery.status,delivery.delivery_time,delivery.delivery_number,delivery.post_code,delivery.post_name,delivery.receive_time')
  454. ->field('order.coin_price,order.rmb_price,order.pay_method,order.pay_time box_open_time')
  455. ->join('order order', 'order.id = delivery.order_id', 'left')
  456. ->where('delivery.status', 'neq', 'unpay')
  457. ->where('delivery.id', $delivery_order_id)
  458. ->where('delivery.user_id', $this->auth->id)
  459. ->find();
  460. if (empty($order)) {
  461. $this->error('订单有误');
  462. }
  463. $order->delivery_apply_time = date('Y-m-d H:i:s', $order->delivery_apply_time);
  464. $order->box_open_time = date('Y-m-d H:i:s', $order->box_open_time);
  465. $order->delivery_time = $order->delivery_time ? date('Y-m-d H:i:s', $order->delivery_time) : '';
  466. $order->receive_time = $order->receive_time ? date('Y-m-d H:i:s', $order->receive_time) : '';
  467. $order->goods_image = $order->goods_image ? cdnurl($order->goods_image, true) : '';
  468. if ('coin' == $order->pay_method) {
  469. $order->pay_amount = $order->coin_price . '金币';
  470. $order->pay_method = '金币';
  471. } else {
  472. $order->pay_amount = $order->rmb_price . '元';
  473. if ('wechat' == $order->pay_method) {
  474. $order->pay_method = '金币';
  475. }
  476. if ('alipay' == $order->pay_method) {
  477. $order->pay_method = '支付宝';
  478. }
  479. }
  480. unset($order->coin_price);
  481. unset($order->rmb_price);
  482. $order->notice = Text::gettext('notice_to_buyers');
  483. $this->success('查询成功', $order);
  484. }
  485. /**
  486. * 确认收货
  487. * @author fuyelk <fuyelk@fuyelk.com>
  488. */
  489. public function confirmReceipt()
  490. {
  491. $delivery_order_id = input('delivery_order_id');
  492. if (empty($delivery_order_id)) {
  493. $this->error('未选择订单');
  494. }
  495. $order = Delivery::field('id,status,prize_id')->where('id', $delivery_order_id)->where('status', 'unreceived')->find();
  496. if (empty($order)) {
  497. $this->error('订单有误');
  498. }
  499. $order->save(['status' => 'finished', 'receive_time' => time()]);
  500. // 更新奖品状态
  501. Prizerecord::where('id', $order->prize_id)->update(['status' => 'received']);
  502. $this->success('确认收货成功');
  503. }
  504. /**
  505. * 我的金币
  506. * @throws \think\exception\DbException
  507. * @author fuyelk <fuyelk@fuyelk.com>
  508. */
  509. public function myCoin()
  510. {
  511. $pagesize = input('pagesize/d', 10);
  512. $page = input('page/d', 1);
  513. $type = input('type');
  514. $type = trim($type);
  515. $whereType = [];
  516. if (!empty($type)) {
  517. if (!in_array($type, ['in', 'out'])) {
  518. $this->error('变动类型只能是:in或out');
  519. }
  520. $cond = 'in' == $type ? 'gt' : 'lt';
  521. $whereType = ['coin' => [$cond, 0]];
  522. }
  523. $typeList = ['pay_box' => '支付盲盒', 'recharge' => '充值', 'from_balance' => '余额转入', 'refund' => '库存不足,支付返回', 'pay_delivery' => '支付运费']; // 变更类型:pay_box=支付盲盒,recharge=充值,from_balance=余额转入,refund=库存不足,支付返回,pay_delivery=支付运费
  524. $list = CoinRecord::field('type,create_time,coin')
  525. ->where('user_id', $this->auth->id)
  526. ->where($whereType)
  527. ->order('id', 'desc')
  528. ->paginate($pagesize, false, ['page' => $page])
  529. ->each(function ($item) use ($typeList) {
  530. $item->create_time = date('Y-m-d H:i:s', $item->create_time);
  531. $item->type = $typeList[$item->type];
  532. $item->coin = $item->coin > 0 ? '+' . $item->coin : strval($item->coin);
  533. });
  534. $ret = [
  535. 'balance' => $this->auth->coin,
  536. 'record' => $list
  537. ];
  538. $this->success('查询成功', $ret);
  539. }
  540. /**
  541. * 我的余额
  542. * @throws \think\exception\DbException
  543. * @author fuyelk <fuyelk@fuyelk.com>
  544. */
  545. public function myBalance()
  546. {
  547. $pagesize = input('pagesize/d', 10);
  548. $page = input('page/d', 1);
  549. $type = input('type');
  550. $type = trim($type);
  551. $whereType = [];
  552. if (!empty($type)) {
  553. if (!in_array($type, ['in', 'out'])) {
  554. $this->error('变动类型只能是:in或out');
  555. }
  556. $cond = 'in' == $type ? 'gt' : 'lt';
  557. $whereType = ['money' => [$cond, 0]];
  558. }
  559. $typeList = [
  560. 'box_exchange' => '盲盒回收',
  561. 'no_stock' => '库存不足,支付返回',
  562. 'withdrawal' => '佣金提现',
  563. 'withdrawals' => '余额提现',
  564. 'to_coin' => '转到钱包',
  565. 'yezhifu' => '余额支付',
  566. 'pay_delivery' => '支付运费',
  567. ];
  568. $list = MoneyRecord::field('type,create_time,money')
  569. ->where('user_id', $this->auth->id)
  570. ->where($whereType)
  571. ->order('id', 'desc')
  572. ->paginate($pagesize, false, ['page' => $page]);
  573. $list = MoneyRecord::field('type,create_time,money')
  574. ->where('user_id', $this->auth->id)
  575. ->where($whereType)
  576. ->order('id', 'desc')
  577. ->paginate($pagesize, false, ['page' => $page])
  578. ->each(function ($item) use ($typeList) {
  579. $item->create_time = date('Y-m-d H:i:s', $item->create_time);
  580. $item->type = $typeList[$item->type];
  581. $item->money = floatval($item->money);
  582. $item->money = $item->money > 0 ? '+' . $item->money : strval($item->money);
  583. });
  584. $ret = [
  585. 'balance' => floatval($this->auth->money),
  586. 'record' => $list
  587. ];
  588. $this->success('查询成功', $ret);
  589. }
  590. /**
  591. * 我的收藏
  592. * @throws \think\exception\DbException
  593. * @author fuyelk <fuyelk@fuyelk.com>
  594. */
  595. public function myStar()
  596. {
  597. $pagesize = input('pagesize/d', 10);
  598. $page = input('page/d', 1);
  599. $list = Star::alias('star')
  600. ->field('star.id star_id,star.create_time star_time')
  601. ->field('box.id box_id,box.box_name,box.coin_price')
  602. ->join('box box', 'box.id = star.box_id')
  603. ->where('star.user_id', $this->auth->id)
  604. ->order('star.id', 'desc')
  605. ->paginate($pagesize, false, ['page' => $page])
  606. ->each(function ($item) {
  607. // 查询盲盒中的第一个商品图片
  608. $goods_id = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->value('goods_id');
  609. $image = Goods::where('id', $goods_id)->value('image');
  610. $item->image = $image ? cdnurl($image, true) : '';
  611. $item->star_time = date('Y-m-d H:i:s', $item->star_time);
  612. });
  613. $this->success('查询成功', $list);
  614. }
  615. /**
  616. * 取消收藏
  617. * @author fuyelk <fuyelk@fuyelk.com>
  618. * @date 2021/07/12 17:29
  619. */
  620. public function cancelStar()
  621. {
  622. $star_id = input('star_id/d');
  623. if (empty($star_id)) {
  624. $this->error('没有选择收藏');
  625. }
  626. Star::where('user_id', $this->auth->id)->where('id', $star_id)->delete();
  627. $this->success('已取消收藏');
  628. }
  629. /**
  630. * 我的收货地址
  631. * @throws \think\db\exception\DataNotFoundException
  632. * @throws \think\db\exception\ModelNotFoundException
  633. * @throws \think\exception\DbException
  634. * @author fuyelk <fuyelk@fuyelk.com>
  635. */
  636. public function myAddress()
  637. {
  638. $is_default = input('is_default/d');
  639. $where_is_default = [];
  640. if (!empty($is_default)) {
  641. $where_is_default = ['is_default' => 1];
  642. }
  643. $list = UserAddress::field('id address_id,username,mobile,province,city,area,detail,is_default')
  644. ->where('user_id', $this->auth->id)
  645. ->where($where_is_default)
  646. ->select();
  647. foreach ($list as $item) {
  648. $item->is_default = !!$item->is_default;
  649. $item->full_address = $item->province . $item->city . $item->area . $item->detail;
  650. }
  651. $this->success('查询成功', $list);
  652. }
  653. /**
  654. * 添加收货地址
  655. * @author fuyelk <fuyelk@fuyelk.com>
  656. */
  657. public function addAddress()
  658. {
  659. $params = input();
  660. $params['user_id'] = $this->auth->id;
  661. $rule = [
  662. 'user_id' => 'require|integer',
  663. 'username' => 'require|max:20',
  664. 'mobile' => 'require|max:15',
  665. 'province' => 'require|max:20',
  666. 'city' => 'require|max:20',
  667. 'area' => 'require|max:20',
  668. 'detail' => 'require|max:50',
  669. 'is_default' => 'integer|in:0,1',
  670. ];
  671. $field = [
  672. 'username' => '收货人姓名',
  673. 'mobile' => '收货人电话',
  674. 'province' => '省份',
  675. 'city' => '市级',
  676. 'area' => '地区',
  677. 'detail' => '详细地址',
  678. 'is_default' => '是否默认',
  679. ];
  680. if (empty($params['is_default'])) {
  681. $params['is_default'] = 0;
  682. }
  683. $validate = new Validate($rule, [], $field);
  684. $result = $validate->check($params);
  685. if (!$result) {
  686. $this->error($validate->getError());
  687. }
  688. $address = new UserAddress();
  689. try {
  690. $res = $address->create($params, array_keys($rule));
  691. if ($params['is_default']) {
  692. UserAddress::where('user_id', $this->auth->id)->where('id', 'neq', $res->id)->update(['is_default' => 0]);
  693. // } else {
  694. // // 没有默认地址,则当前地址设为默认地址
  695. // if (!UserAddress::where('user_id', $this->auth->id)->where('is_default', 1)->value('id')) {
  696. // $res->save(['is_default' => 1]);
  697. // }
  698. }
  699. } catch (\Exception $e) {
  700. $this->error($e->getMessage());
  701. }
  702. $this->success('添加成功');
  703. }
  704. /**
  705. * 编辑收货地址
  706. * @author fuyelk <fuyelk@fuyelk.com>
  707. */
  708. public function editAddress()
  709. {
  710. $params = input();
  711. $params['user_id'] = $this->auth->id;
  712. $rule = [
  713. 'address_id' => 'require|integer',
  714. 'user_id' => 'require|integer',
  715. 'username' => 'require|max:20',
  716. 'mobile' => 'require|max:15',
  717. 'province' => 'require|max:20',
  718. 'city' => 'require|max:20',
  719. 'area' => 'require|max:20',
  720. 'detail' => 'require|max:50',
  721. 'is_default' => 'integer|in:0,1',
  722. ];
  723. $field = [
  724. 'address_id' => '地址ID',
  725. 'username' => '收货人姓名',
  726. 'mobile' => '收货人电话',
  727. 'province' => '省份',
  728. 'city' => '市级',
  729. 'area' => '地区',
  730. 'detail' => '详细地址',
  731. 'is_default' => '是否默认',
  732. ];
  733. if (empty($params['is_default'])) {
  734. $params['is_default'] = 0;
  735. }
  736. $validate = new Validate($rule, [], $field);
  737. $result = $validate->check($params);
  738. if (!$result) {
  739. $this->error($validate->getError());
  740. }
  741. $row = UserAddress::where('user_id', $this->auth->id)->where('id', $params['address_id'])->find();
  742. if (empty($row)) {
  743. $this->error('地址不存在');
  744. }
  745. unset($params['address_id']);
  746. unset($rule['address_id']);
  747. try {
  748. $row->allowfield(array_keys($rule))->save($params);
  749. if ($params['is_default']) {
  750. UserAddress::where('user_id', $this->auth->id)->where('id', 'neq', $row->id)->update(['is_default' => 0]);
  751. // } else {
  752. // // 没有默认地址,则当前地址设为默认地址
  753. // if (!UserAddress::where('user_id', $this->auth->id)->where('is_default', 1)->value('id')) {
  754. // $row->save(['is_default' => 1]);
  755. // }
  756. }
  757. } catch (\Exception $e) {
  758. $this->error($e->getMessage());
  759. }
  760. $this->success('修改成功');
  761. }
  762. /**
  763. * 删除收货地址
  764. * @author fuyelk <fuyelk@fuyelk.com>
  765. */
  766. public function deleteAddress()
  767. {
  768. $id = input('address_id/d');
  769. if (empty($id)) {
  770. $this->error('未选择地址');
  771. }
  772. try {
  773. $row = UserAddress::where('user_id', $this->auth->id)->where('id', $id)->find();
  774. } catch (\Exception $e) {
  775. $this->error('删除失败');
  776. }
  777. if (empty($row)) {
  778. $this->error('地址不存在');
  779. }
  780. $row->delete();
  781. $this->success('删除成功');
  782. }
  783. /**
  784. * 获取设置信息
  785. * @author fuyelk <fuyelk@fuyelk.com>
  786. */
  787. public function getSettingInfo()
  788. {
  789. $ret = [
  790. 'avatar' => $this->auth->avatar ? cdnurl($this->auth->avatar, true) : letter_avatar($this->auth->nickname),
  791. 'avatar_url' => $this->auth->avatar,
  792. 'nickname' => $this->auth->nickname
  793. ];
  794. $this->success('查询成功', $ret);
  795. }
  796. /**
  797. * 检查新版本
  798. * @author fuyelk <fuyelk@fuyelk.com>
  799. */
  800. public function checkVersion()
  801. {
  802. $currentVersion = input('current_version');
  803. $platform = input('platform');
  804. if (!in_array($platform, ['h5', 'android', 'ios', 'applets'])) {
  805. $this->error('选择的平台有误');
  806. }
  807. $lastVersion = Version::getLatest($currentVersion, $platform);
  808. $ret = [
  809. 'has_new' => false,
  810. 'new_version' => '',
  811. 'enforce' => '',
  812. 'download_url' => '',
  813. 'content' => '',
  814. ];
  815. // 发现新版本
  816. $hasNew = false;
  817. if ($lastVersion) {
  818. $hasNew = version_compare($currentVersion, $lastVersion->version, '<');
  819. }
  820. $msg = $hasNew ? '发现新版本' : '已是最新版本';
  821. if ($hasNew) {
  822. $ret = [
  823. 'has_new' => $hasNew,
  824. 'new_version' => $lastVersion->version,
  825. 'enforce' => !!$lastVersion->enforce,
  826. 'download_url' => cdnurl($lastVersion->download_file, true),
  827. 'content' => $lastVersion->content,
  828. ];
  829. }
  830. $this->success($msg, $ret);
  831. }
  832. /**
  833. * 修改个人信息
  834. * @author fuyelk <fuyelk@fuyelk.com>
  835. * @date 2021/07/13 14:52
  836. */
  837. public function changeInfo()
  838. {
  839. $params = input('post.');
  840. if (isset($params['avatar_url'])) {
  841. $params['avatar'] = $params['avatar_url'];
  842. }
  843. unset($params['avatar_url']);
  844. if (empty($params)) {
  845. $this->error('未修改任何内容');
  846. }
  847. $rule = [
  848. 'avatar' => 'min:1',
  849. 'nickname' => 'min:1|max:30'
  850. ];
  851. $field = [
  852. 'avatar' => '头像',
  853. 'nickname' => '昵称'
  854. ];
  855. $validate = new Validate($rule, [], $field);
  856. $result = $validate->check($params);
  857. if (!$result) {
  858. $this->error($validate->getError());
  859. }
  860. $user = $this->auth->getUser();
  861. $user->allowField(array_keys($rule))->save($params);
  862. $this->success('保存成功');
  863. }
  864. /**
  865. * 检查发货信息
  866. * @author fuyelk <fuyelk@fuyelk.com>
  867. */
  868. public function checkDeliveryInfo()
  869. {
  870. $prize_record_id = input('record_ids');
  871. if (empty($prize_record_id)) {
  872. $this->error('未选择盲盒商品');
  873. }
  874. $prizeIds = explode(',', trim($prize_record_id, ','));
  875. $prizeIds = array_unique($prizeIds);
  876. $orderInfo = [];
  877. $delivery_fee = 0;
  878. // 检查选择的每一个奖品
  879. foreach ($prizeIds as $prizeId) {
  880. if (!is_numeric($prizeId)) {
  881. $this->error('盲盒ID有误');
  882. }
  883. // 检查奖品是否有效
  884. $prize = Prizerecord::alias('prize')
  885. ->field('prize.id record_id,prize.goods_name,prize.goods_image,prize.delivery_fee,prize.create_time')
  886. ->field('order.coin_price box_coin_price,order.rmb_price box_rmb_price,order.pay_method')
  887. ->join('order order', 'order.id = prize.order_id')
  888. ->where('prize.user_id', $this->auth->id)
  889. ->where('prize.id', $prizeId)
  890. ->where('prize.status', 'bag') // 奖品状态:bag=盒柜,exchange=已回收,delivery=申请发货,received=已收货
  891. ->find();
  892. if (empty($prize)) {
  893. $this->error('选择的盲盒有误');
  894. }
  895. $prize->goods_image = $prize->goods_image ? cdnurl($prize->goods_image, true) : '';
  896. $prize->time = date('Y-m-d H:i:s', $prize->create_time);
  897. $delivery_fee += $prize->delivery_fee;
  898. $prize->delivery_fee = '¥' . $prize->delivery_fee;
  899. $pay_method_list = [
  900. 'coin' => '金币',
  901. 'wechat' => '微信',
  902. 'alipay' => '支付宝',
  903. 'money'=>'余额'
  904. ];
  905. if ('coin' == $prize->pay_method) {
  906. $prize->pay_amount = $prize->box_coin_price . '金币';
  907. } else {
  908. $prize->pay_amount = $prize->box_rmb_price . '元';
  909. }
  910. if($prize->pay_method ==NULL){
  911. $prize->pay_method = 'alipay';
  912. }
  913. $prize->pay_method = $pay_method_list[$prize->pay_method];
  914. unset($prize->box_rmb_price);
  915. unset($prize->box_coin_price);
  916. unset($prize->create_time);
  917. $orderInfo[] = $prize->toArray();
  918. }
  919. $ret = [
  920. 'total_delivery_fee' => $delivery_fee,
  921. 'order_info' => $orderInfo
  922. ];
  923. $this->success('查询成功', $ret);
  924. }
  925. /**
  926. * 申请发货
  927. * @author fuyelk <fuyelk@fuyelk.com>
  928. */
  929. public function applyDelivery()
  930. {
  931. $address_id = input('address_id');
  932. $prize_record_id = input('record_ids');
  933. if (empty($address_id)) {
  934. $this->error('未选择收货地址');
  935. }
  936. if (empty($prize_record_id)) {
  937. $this->error('未选择盲盒商品');
  938. }
  939. // 检查收货地址
  940. $address = UserAddress::field('username,mobile,province,city,area,detail')
  941. ->where('id', $address_id)
  942. ->where('user_id', $this->auth->id)
  943. ->find();
  944. if (empty($address)) {
  945. $this->error('收货地址有误');
  946. }
  947. $prizeIds = explode(',', trim($prize_record_id, ','));
  948. $prizeIds = array_unique($prizeIds);
  949. $delivery_fee = 0;
  950. $trade = null;
  951. // 检查选择的每一个奖品
  952. foreach ($prizeIds as $prizeId) {
  953. if (!is_numeric($prizeId)) {
  954. $this->error('盲盒ID有误');
  955. }
  956. // 检查奖品是否有效
  957. $prize = Prizerecord::field('id,delivery_fee')
  958. ->where('id', $prizeId)
  959. ->where('user_id', $this->auth->id)
  960. ->where('status', 'bag') // 奖品状态:bag=盒柜,exchange=已回收,delivery=申请发货,received=已收货
  961. ->find();
  962. if (empty($prize)) {
  963. $this->error('选择的盲盒有误');
  964. }
  965. $delivery_fee += floatval($prize->delivery_fee);
  966. }
  967. // $delivery_fee = 0.1;
  968. Db::startTrans();
  969. try {
  970. // 如果需要支付的话 订单为未支付状态 拉起支付 否则 改为待发货状态
  971. if($delivery_fee > 0){
  972. $deliveryStatus = 'unpay';
  973. }else{
  974. $deliveryStatus = 'paid';
  975. }
  976. // 新加记录用户积分日志
  977. $user= Db::table('box_user') ->where('id',$this->auth->id)->find();
  978. $yq=$user['coin'];
  979. $coinAmount = ceil(Setting::getCoinFromRmb(round($delivery_fee, 2)));
  980. $jq=$coinAmount; // 增加的积分
  981. $hq=$yq+$jq;
  982. if($delivery_fee <= 0){
  983. Db::table('box_user_score_log')->insertGetId([
  984. 'user_id' => $user['id'],
  985. 'score' => $jq,
  986. 'before' => $yq,
  987. 'after' => $hq,
  988. 'memo' => '商品发货',
  989. 'createtime' =>time()
  990. ]);
  991. }
  992. // 创建发货交易订单
  993. $trade = DeliveryTrade::create([
  994. 'user_id' => $this->auth->id,
  995. 'rmb_amount' => round($delivery_fee, 2),
  996. 'coin_amount' => $coinAmount,
  997. 'status' => $deliveryStatus,
  998. 'out_trade_no' => date('YmdHis') . mt_rand(10000, 99999)
  999. ]);
  1000. // 新加清空购物车
  1001. foreach ($prizeIds as $prizeId) {
  1002. $prize = Prizerecord::where('id', $prizeId)->find();
  1003. // [id] => 1992
  1004. // [box_id] => 48
  1005. // [order_id] => 1453
  1006. // [out_trade_no] => 202201172320492762153
  1007. // [user_id] => 191
  1008. // [goods_id] => 308
  1009. // [goods_name] => u盘32g高速usb
  1010. // [goods_image] => http://h5mhsc.oss-cn-beijing.aliyuncs.com/d3c7b4b9f0bbc3717250d2e0e77d83fa.jpg
  1011. // [goods_coin_price] => 45
  1012. // [goods_rmb_price] => 11.25
  1013. // [status] => bag
  1014. // [exchange_time] =>
  1015. // [delivery_time] =>
  1016. // [delivery_fee] => 1.00
  1017. // [create_time] => 1642432852
  1018. // [update_time] => 1642432852
  1019. // [delete_time] =>
  1020. // [zz_time] => 0
  1021. if($deliveryStatus == 'unpay'){
  1022. // 创建发货订单
  1023. Delivery::create([
  1024. 'order_id' => $prize->order_id,
  1025. 'out_trade_no' => $prize->out_trade_no,
  1026. 'delivery_trade_id' => $trade->id,
  1027. 'delivery_order_no' => date('YmdHis') . mt_rand(1000, 9999),
  1028. 'prize_id' => $prize->id,
  1029. 'goods_name' => $prize->goods_name,
  1030. 'goods_image' => $prize->goods_image,
  1031. 'user_id' => $this->auth->id,
  1032. 'username' => $address->username,
  1033. 'mobile' => $address->mobile,
  1034. 'province' => $address->province,
  1035. 'city' => $address->city,
  1036. 'area' => $address->area,
  1037. 'detail' => $address->detail,
  1038. 'address' => $address->province . $address->city . $address->area . $address->detail,
  1039. ]);
  1040. }else{
  1041. Db::table('box_prize_record')->where('id', $prizeId)->setField('status','delivery');
  1042. // 新加创建待发货订单
  1043. Delivery::create([
  1044. 'order_id' => $prize->order_id,
  1045. 'out_trade_no' => $prize->out_trade_no,
  1046. 'delivery_trade_id' => $trade->id,
  1047. 'delivery_order_no' => date('YmdHis') . mt_rand(1000, 9999),
  1048. 'prize_id' => $prize->id,
  1049. 'goods_name' => $prize->goods_name,
  1050. 'goods_image' => $prize->goods_image,
  1051. 'user_id' => $this->auth->id,
  1052. 'username' => $address->username,
  1053. 'mobile' => $address->mobile,
  1054. 'province' => $address->province,
  1055. 'city' => $address->city,
  1056. 'area' => $address->area,
  1057. 'detail' => $address->detail,
  1058. 'address' => $address->province . $address->city . $address->area . $address->detail,
  1059. 'status' => 'undelivered' // 0运费改为待发货状态
  1060. ]);
  1061. }
  1062. }
  1063. } catch (\Exception $e) {
  1064. Db::rollback();
  1065. dta(['user_id', $this->auth->id, 'prize_ids' => $prizeIds, 'error' => $e->getMessage()], '用户申请发货失败');
  1066. $this->error('申请发货失败');
  1067. }
  1068. Db::commit();
  1069. $ret = [
  1070. 'order_id' => intval($trade->id),
  1071. 'alipay' => $this->request->domain() . '/api/alipay/deliverypay/orderid/' . intval($trade->id),
  1072. 'wechat' => '/api/wechat/deliverypay/orderid/' . intval($trade->id),
  1073. 'delivery_fee' => $delivery_fee
  1074. ];
  1075. $this->success('创建订单成功', $ret);
  1076. }
  1077. /**
  1078. * 金币支付发货订单
  1079. * @author fuyelk <fuyelk@fuyelk.com>
  1080. * @date 2021/07/22 17:12
  1081. */
  1082. public function coinPayDelivery()
  1083. {
  1084. $order_id = input('order_id/d');
  1085. if (empty($order_id)) {
  1086. $this->error('请选择支付订单');
  1087. }
  1088. $trade = DeliveryTrade::where('id', $order_id)->where('user_id', $this->auth->id)->find();
  1089. if (empty($trade)) {
  1090. $this->error('订单不存在');
  1091. }
  1092. if ('unpay' != $trade->status) {
  1093. $this->error('该订单已支付,请勿重复支付');
  1094. }
  1095. // 查询用户余额
  1096. if (intval($this->auth->coin) < $trade->coin_amount) {
  1097. $this->error('您的金币不足');
  1098. }
  1099. Db::startTrans();
  1100. try {
  1101. // 更新订单信息
  1102. $trade->pay_method = 'coin';
  1103. $trade->pay_coin = $trade->coin_amount;
  1104. $trade->pay_time = time();
  1105. $trade->status = 'paid';// 状态:unpay=待支付,paid=已支付
  1106. $trade->save();
  1107. $coin_before = $this->auth->coin;
  1108. // 减少金币余额
  1109. $user = $this->auth->getUser();
  1110. $user->setDec('coin', $trade->pay_coin);
  1111. // 创建金币使用记录
  1112. CoinRecord::create([
  1113. 'user_id' => $this->auth->id,
  1114. 'before' => $coin_before,
  1115. 'after' => $this->auth->coin,
  1116. 'coin' => -$trade->pay_coin,
  1117. 'order_id' => $trade->id,
  1118. 'type' => 'pay_delivery', // 变更类型:pay_box=支付盲盒,recharge=充值,fromwallet=余额转入,pay_delivery=支付运费
  1119. ]);
  1120. // 变更发货订单状态
  1121. $deliveryOrder = Delivery::where('delivery_trade_id', $trade->id)->select();
  1122. $prizeIds = [];
  1123. foreach ($deliveryOrder as $order) {
  1124. $order->save(['status' => 'undelivered']);
  1125. $prizeIds[] = $order->prize_id;
  1126. }
  1127. // 变更奖品状态
  1128. Prizerecord::whereIn('id', $prizeIds)->update(['status' => 'delivery', 'delivery_time' => time()]);
  1129. } catch (\Exception $e) {
  1130. Db::rollback();
  1131. dta('用户支付失败,订单已回滚到待支付');
  1132. $this->error('支付失败');
  1133. }
  1134. Db::commit();
  1135. $this->success('支付成功');
  1136. }
  1137. /**
  1138. * 盒机回收
  1139. * @author fuyelk <fuyelk@fuyelk.com>
  1140. */
  1141. public function exchange()
  1142. {
  1143. $prize_record_id = input('record_ids');
  1144. if (empty($prize_record_id)) {
  1145. $this->error('未选择盲盒商品');
  1146. }
  1147. $prizeIds = explode(',', trim($prize_record_id, ','));
  1148. $prizeIds = array_unique($prizeIds);
  1149. // 检查选择的每一个奖品
  1150. foreach ($prizeIds as $prizeId) {
  1151. if (!is_numeric($prizeId)) {
  1152. $this->error('盲盒ID有误');
  1153. }
  1154. // 检查奖品是否有效
  1155. $prize = Prizerecord::field('id')
  1156. ->where('user_id', $this->auth->id)
  1157. ->where('id', $prizeId)
  1158. ->where('status', 'bag') // 奖品状态:bag=盒柜,exchange=已回收,delivery=申请发货,received=已收货
  1159. ->find();
  1160. if (empty($prize)) {
  1161. $this->error('选择的盲盒有误');
  1162. }
  1163. }
  1164. $total = 0;
  1165. $prizeCount = [];
  1166. $prizeInfo = [];
  1167. Db::startTrans();
  1168. try {
  1169. $user = $this->auth->getUser();
  1170. foreach ($prizeIds as $prizeId) {
  1171. $prize = Prizerecord::where('id', $prizeId)->find();
  1172. // 更新盒柜
  1173. $prize->save(['status' => 'exchange', 'exchange_time' => time()]); // 奖品状态:bag=盒柜,exchange=已回收,delivery=申请发货,received=已收货
  1174. // 回收前用户余额
  1175. $money_before = $this->auth->money;
  1176. // 查询回收折扣
  1177. $recovery_discount = floatval(Setting::getSetting('recovery_discount'));
  1178. // 计算折扣后金额
  1179. $amount = floor($prize->goods_coin_price * round($recovery_discount / 100, 2));
  1180. $total += $amount;
  1181. // 统计回收信息
  1182. if (empty($prizeCount[$prize->goods_id])) {
  1183. $prizeCount[$prize->goods_id] = 1;
  1184. } else {
  1185. $prizeCount[$prize->goods_id]++;
  1186. }
  1187. $prizeInfo[$prize->goods_id] = [
  1188. 'name' => $prize->goods_name,
  1189. 'num' => $prizeCount[$prize->goods_id],
  1190. 'price' => $amount
  1191. ];
  1192. // 增加余额
  1193. $user->setInc('money', $amount);
  1194. // 创建余额记录
  1195. MoneyRecord::create([
  1196. 'user_id' => $this->auth->id,
  1197. 'before' => $money_before,
  1198. 'after' => $this->auth->money,
  1199. 'money' => $amount,
  1200. 'prize_id' => $prize->id,
  1201. 'type' => 'box_exchange', // 变更类型:box_exchange=盲盒回收,refund=库存不足,支付返回,withdrawal=提现,to_coin=转到钱包,withdrawal_fail=提现失败,pay_delivery=支付运费
  1202. ]);
  1203. }
  1204. } catch (\Exception $e) {
  1205. Db::rollback();
  1206. dta(['user_id', $this->auth->id, 'prize_ids' => $prizeIds, 'error' => $e->getMessage()], '用户申请发货失败');
  1207. $this->error('回收失败');
  1208. }
  1209. Db::commit();
  1210. $ret = [
  1211. 'amount' => $total,
  1212. 'goods_info' => array_values($prizeInfo),
  1213. 'notice' => Text::getText('recovery_rule')
  1214. ];
  1215. $this->success('回收成功', $ret);
  1216. }
  1217. public function echargez()
  1218. {
  1219. $prizeId = input('record_ids');
  1220. $phone = input('mobile');
  1221. $time = time() - 86400;
  1222. // 检查奖品是否有效
  1223. $prize = Prizerecord::field('id')
  1224. ->where('user_id', $this->auth->id)
  1225. ->where('id', $prizeId)
  1226. ->where('status', 'bag')
  1227. ->where('zz_time','<', $time)
  1228. // 奖品状态:bag=盒柜,exchange=已回收,delivery=申请发货,received=已收货
  1229. ->find();
  1230. if (empty($prize)) {
  1231. $this->error('盲盒失效或者未满24小时无法进行操作!');
  1232. }
  1233. $users = \app\common\model\User::where('mobile',$phone)->find();
  1234. if($users){
  1235. $mobile = $this->auth->mobile;
  1236. $prize = Prizerecord::where('id', $prizeId)->find();
  1237. // 更新盒柜
  1238. if($phone == $mobile){
  1239. $this->error('不能转赠给自己!');
  1240. }
  1241. $prize->save(['user_id'=>$users['id'],'zz_time' => time()]);
  1242. $times = date("Y-m-d H:i:s",time());
  1243. $data = [
  1244. 'boxid' => $prizeId,
  1245. 'boxgoods' => $prize['goods_name'],
  1246. 'zzuser' => $mobile,
  1247. 'szuser' => $phone,
  1248. 'zzid' => $this->auth->id,
  1249. 'zhuanztime' => $times,
  1250. 'zzimage' =>$prize['goods_image']
  1251. ];
  1252. if(db('zz')->insert($data)){
  1253. $this->success('转增成功');
  1254. }else{
  1255. $this->error('转增失效');
  1256. }}else{
  1257. $this->error('手机号未注册!');
  1258. }
  1259. }
  1260. public function echargezs()
  1261. {
  1262. $user = $this->auth->id;
  1263. $data = db('zz')->where('zzid', $user)->select();
  1264. $this->success('转增成功',$data);
  1265. }
  1266. /**
  1267. * 搜索历史
  1268. * @author fuyelk <fuyelk@fuyelk.com>
  1269. */
  1270. public function searchHistory()
  1271. {
  1272. $list = SearchHistory::where('user_id', $this->auth->id)->column('search');
  1273. $this->success('查询成功', ['history' => $list]);
  1274. }
  1275. /**
  1276. * 清除搜索历史
  1277. * @author fuyelk <fuyelk@fuyelk.com>
  1278. */
  1279. public function clearHistory()
  1280. {
  1281. SearchHistory::where('user_id', $this->auth->id)->delete();
  1282. $this->success('清除成功');
  1283. }
  1284. /**
  1285. * 余额转出到金币
  1286. * @author fuyelk <fuyelk@fuyelk.com>
  1287. * @date 2021/07/15 14:44
  1288. */
  1289. public function moneyToCoin()
  1290. {
  1291. $amount = input('amount/d');
  1292. if (!is_numeric($amount) || empty($amount)) {
  1293. $this->error('金额只能是大于0的整数');
  1294. }
  1295. Db::startTrans();
  1296. try {
  1297. $user = $this->auth->getUser();
  1298. $coin_before = $this->auth->coin;
  1299. $money_before = $this->auth->money;
  1300. // 检查余额够不够
  1301. if ($money_before < $amount) {
  1302. Db::rollback();
  1303. $this->error('余额不足');
  1304. }
  1305. // 增加金币
  1306. $user->setInc('coin', intval($amount));
  1307. // 创建金币记录
  1308. CoinRecord::create([
  1309. 'user_id' => $this->auth->id,
  1310. 'before' => $coin_before,
  1311. 'after' => $this->auth->coin,
  1312. 'coin' => $amount,
  1313. 'type' => 'from_balance', // 变更类型:pay_box=支付盲盒,recharge=充值,from_balance=余额转入,refund=库存不足,支付返回,pay_delivery=支付运费
  1314. ]);
  1315. // 减少余额
  1316. $user->setDec('money', intval($amount));
  1317. // 创建余额记录
  1318. MoneyRecord::create([
  1319. 'user_id' => $this->auth->id,
  1320. 'before' => $money_before,
  1321. 'after' => $this->auth->money,
  1322. 'money' => -$amount,
  1323. 'type' => 'to_coin', // 变更类型:box_exchange=盲盒回收,refund=库存不足,支付返回,withdrawal=提现,to_coin=转到钱包,withdrawal_fail=提现失败
  1324. ]);
  1325. } catch (\Exception $e) {
  1326. Db::rollback();
  1327. $this->error('转出失败');
  1328. }
  1329. Db::commit();
  1330. $this->success('转出到钱包成功');
  1331. }
  1332. /**
  1333. * 申请提现
  1334. * @author fuyelk <fuyelk@fuyelk.com>
  1335. */
  1336. public function withdrawal()
  1337. {
  1338. $amount = input('amount/d');
  1339. $type = input('type');
  1340. if (!is_numeric($amount) || empty($amount) || $amount < 10) {
  1341. $this->error('金额有误最低提现10元');
  1342. }
  1343. if (!in_array($type, ['wechat', 'alipay'])) {
  1344. $this->error('提现方式有误');
  1345. }
  1346. $user = $this->auth->getUser();
  1347. if ('wechat' == $type) {
  1348. if (empty($user->wechat_username) || empty($user->wechat_account)) {
  1349. $this->error('请先绑定微信提现账户');
  1350. }
  1351. $username = $user->wechat_username;
  1352. $account = $user->wechat_account;
  1353. } else {
  1354. if (empty($user->alipay_username) || empty($user->alipay_account)) {
  1355. $this->error('请先绑定支付宝提现账户');
  1356. }
  1357. $username = $user->alipay_username;
  1358. $account = $user->alipay_account;
  1359. }
  1360. Db::startTrans();
  1361. try {
  1362. $user = $this->auth->getUser();
  1363. $money_before = $this->auth->retail_balance;
  1364. // 检查余额够不够
  1365. if ($money_before < $amount) {
  1366. Db::rollback();
  1367. $this->error('余额不足');
  1368. }
  1369. // 减少余额
  1370. $user->setDec('retail_balance', intval($amount));
  1371. // 创建余额记录
  1372. // MoneyRecord::create([
  1373. // 'user_id' => $this->auth->id,
  1374. // 'before' => $money_before,
  1375. // 'after' => $this->auth->retail_balance,
  1376. // 'money' => -$amount,
  1377. // 'type' => 'withdrawal', // 变更类型:box_exchange=盲盒回收,refund=库存不足,支付返回,withdrawal=提现,to_coin=转到钱包,withdrawal_fail=提现失败
  1378. // ]);
  1379. // 创建提现申请
  1380. Withdrawal::create([
  1381. 'user_id' => $this->auth->id,
  1382. 'amount' => $amount,
  1383. 'type' => $type,
  1384. 'username' => $username,
  1385. 'account' => $account,
  1386. 'drawal_type' => 1,
  1387. ]);
  1388. } catch (\Exception $e) {
  1389. Db::rollback();
  1390. $this->error('佣金提现失败');
  1391. }
  1392. Db::commit();
  1393. $this->success('佣金提现成功');
  1394. }
  1395. public function withdrawals()
  1396. {
  1397. $amount = input('amount/d');
  1398. $type = input('type');
  1399. if (!is_numeric($amount) || empty($amount) || $amount < 10) {
  1400. $this->error('金额有误最低提现10元');
  1401. }
  1402. if (!in_array($type, ['wechat', 'alipay'])) {
  1403. $this->error('提现方式有误');
  1404. }
  1405. $user = $this->auth->getUser();
  1406. if ('wechat' == $type) {
  1407. if (empty($user->wechat_username) || empty($user->wechat_account)) {
  1408. $this->error('请先绑定微信提现账户');
  1409. }
  1410. $username = $user->wechat_username;
  1411. $account = $user->wechat_account;
  1412. } else {
  1413. if (empty($user->alipay_username) || empty($user->alipay_account)) {
  1414. $this->error('请先绑定支付宝提现账户');
  1415. }
  1416. $username = $user->alipay_username;
  1417. $account = $user->alipay_account;
  1418. }
  1419. Db::startTrans();
  1420. try {
  1421. $user = $this->auth->getUser();
  1422. $money_before = $this->auth->money;
  1423. // 检查余额够不够
  1424. if ($money_before < $amount) {
  1425. Db::rollback();
  1426. $this->error('余额不足');
  1427. }
  1428. // 减少余额
  1429. $user->setDec('money', intval($amount));
  1430. // 创建余额记录
  1431. MoneyRecord::create([
  1432. 'user_id' => $this->auth->id,
  1433. 'before' => $money_before,
  1434. 'after' => $this->auth->money,
  1435. 'money' => -$amount,
  1436. 'type' => 'withdrawals', // 变更类型:box_exchange=盲盒回收,refund=库存不足,支付返回,withdrawal=提现,to_coin=转到钱包,withdrawal_fail=提现失败
  1437. ]);
  1438. // 创建提现申请
  1439. Withdrawal::create([
  1440. 'user_id' => $this->auth->id,
  1441. 'amount' => $amount,
  1442. 'type' => $type,
  1443. 'username' => $username,
  1444. 'account' => $account,
  1445. 'drawal_type' => 0,
  1446. ]);
  1447. } catch (\Exception $e) {
  1448. Db::rollback();
  1449. $this->error('余额提现失败');
  1450. }
  1451. Db::commit();
  1452. $this->success('余额提现成功');
  1453. }
  1454. /**
  1455. * 获取提现绑定信息
  1456. * @author fuyelk <fuyelk@fuyelk.com>
  1457. */
  1458. public function getWithdrawalSetting()
  1459. {
  1460. $user = $this->auth->getUser();
  1461. $ret = [
  1462. 'wechat' => [
  1463. 'username' => $user->wechat_username ?: '',
  1464. 'account' => $user->wechat_account ?: ''
  1465. ],
  1466. 'alipay' => [
  1467. 'username' => $user->alipay_username ?: '',
  1468. 'account' => $user->alipay_account ?: ''
  1469. ]
  1470. ];
  1471. $this->success('查询成功', $ret);
  1472. }
  1473. /**
  1474. * 获取提现绑定信息
  1475. * @author fuyelk <fuyelk@fuyelk.com>
  1476. */
  1477. public function bindWithdrawalAccount()
  1478. {
  1479. $type = input('type');
  1480. $username = input('username');
  1481. $account = input('account');
  1482. if (!in_array($type, ['wechat', 'alipay'])) {
  1483. $this->error('绑定类型有误');
  1484. }
  1485. if (empty($username) || empty($account)) {
  1486. $this->error('姓名和账户不能为空');
  1487. }
  1488. if ('wechat' == $type) {
  1489. $update = [
  1490. 'wechat_username' => $username,
  1491. 'wechat_account' => $account
  1492. ];
  1493. } else {
  1494. $update = [
  1495. 'alipay_username' => $username,
  1496. 'alipay_account' => $account
  1497. ];
  1498. }
  1499. $user = $this->auth->getUser();
  1500. try {
  1501. $user->save($update);
  1502. } catch (Exception $e) {
  1503. $this->error('信息有误');
  1504. }
  1505. $this->success('綁定成功');
  1506. }
  1507. /**
  1508. * 快递、物流信息
  1509. * @author fuyelk <fuyelk@fuyelk.com>
  1510. */
  1511. public function express()
  1512. {
  1513. $order_id = input('delivery_order_id');
  1514. if (empty($order_id)) {
  1515. $this->error('未选择订单');
  1516. }
  1517. $list = [];
  1518. $order = Delivery::field('delivery_number,post_name,post_code')
  1519. ->where('id', $order_id)
  1520. ->where('user_id', $this->auth->id)
  1521. ->where('status', 'neq', 'undelivered') // 发货状态:undelivered=待发货,unreceived=待收货,finished=已完成
  1522. ->find();
  1523. if (empty($order)) {
  1524. $this->success('未查询到快递信息', ['list' => $list]);
  1525. }
  1526. // TODO 对接第三方物流接口
  1527. // ...
  1528. $list = [
  1529. [
  1530. 'time' => '2018-10-13 13:01:10',
  1531. 'desc' => '您已签收本次订单的包裹,本次配送完成'
  1532. ],
  1533. [
  1534. 'time' => '2018-10-13 10:01:10',
  1535. 'desc' => '快递到达某某地'
  1536. ],
  1537. [
  1538. 'time' => '2018-10-13 10:01:10',
  1539. 'desc' => '快递到达某某地'
  1540. ],
  1541. [
  1542. 'time' => '2018-10-13 10:01:10',
  1543. 'desc' => '快递到达某某地'
  1544. ],
  1545. ];
  1546. $this->success('查询成功', ['list' => $list]);
  1547. }
  1548. }