Shop.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 商城管理
  7. */
  8. namespace app\green\controller\api\v1;
  9. use app\green\controller\api\Base;
  10. use app\common\facade\WechatPay;
  11. use app\common\model\SystemUserAddress;
  12. use app\green\model\GreenShop;
  13. use app\green\model\GreenUser;
  14. use app\green\model\GreenOrder;
  15. use think\facade\Request;
  16. class Shop extends Base{
  17. /**
  18. * 读取某个分类下的商品列表
  19. * @param integer api 读取ID
  20. * @return json
  21. */
  22. public function list(){
  23. $data['page'] = Request::param('page',0);
  24. $data['sign'] = Request::param('sign');
  25. $rel = $this->apiSign($data);
  26. if($rel['code'] == 200){
  27. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  28. $condition[] = ['is_sale','=',1];
  29. $condition[] = ['is_del','=',0];
  30. $result = GreenShop::where($condition)->field('id,name,points,note,img')->order('sort desc,id desc')->paginate(20,true)->toArray();
  31. if(empty($result['data'])){
  32. return enjson(204,'没有内容了');
  33. }
  34. return enjson(200,'成功',$result['data']);
  35. }
  36. return enjson($rel['code'],'签名验证失败');
  37. }
  38. /**
  39. * 读取单个商品信息
  40. * @param integer $id 商品ID
  41. * @return void
  42. */
  43. public function item(int $id){
  44. $param['id'] = Request::param('id',0);
  45. $param['sign'] = Request::param('sign');
  46. $rel = $this->apiSign($param);
  47. if($rel['code'] == 200){
  48. //查找商品SPU
  49. $item = GreenShop::where(['is_sale'=>1,'is_del' => 0,'id' => $id,'member_miniapp_id' => $this->miniapp_id])->field('id,name,note,points,img,imgs,content')->find();
  50. if(empty($item)){
  51. return json(['code'=>403,'msg'=>'没有内容']);
  52. }
  53. $data = [];
  54. $data['content'] = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($item->content));
  55. $data['name'] = $item->name;
  56. $data['note'] = $item->note;
  57. $data['points'] = $item->points;
  58. $data['img'] = $item->img.'?x-oss-process = style/500';
  59. $data['imgs'] = json_decode($item->imgs,true);
  60. return enjson(200,'成功',$data);
  61. }
  62. return enjson(204,'签名错误');
  63. }
  64. //立即兑换
  65. public function dopay(){
  66. $this->isUserAuth();
  67. if (request()->isPost()) {
  68. $param['shop_id'] = $this->request->param('shop_id/d');
  69. $param['message'] = $this->request->param('message','');
  70. $param['address'] = $this->request->param('address/d');
  71. $param['ucode'] = $this->request->param('ucode','');
  72. $param['signkey'] = $this->request->param('signkey');
  73. $param['sign'] = $this->request->param('sign');
  74. $sign = $this->apiSign($param);
  75. if($sign['code'] != 200){
  76. return enjson($sign['code'],'签名验证失败');
  77. }
  78. //读取发货地址
  79. $address = SystemUserAddress::where(['user_id'=>$this->user->id,'id' =>$param['address']])->find();
  80. if(empty($address)){
  81. return enjson(403,'请选择收货地址');
  82. }
  83. //判断是否已下架
  84. $item = GreenShop::where(['id' => $param['shop_id'],'is_sale' => 1,'is_del' => 0])->field('id,points,name,note,img,imgs,content')->find();
  85. if(empty($item)){
  86. return enjson(403,'活动已下架');
  87. }
  88. if(time() > $item->end_time){
  89. return enjson(403,'活动已结束,还可以参加其它活动.');
  90. }
  91. //判断积分是否足够
  92. $bank = GreenUser::where(['member_miniapp_id'=>$this->miniapp_id,'uid' => $this->user->id])->find();
  93. if(empty($bank) || $bank->points < $item->points){
  94. return enjson(403,'积分不足够支付');
  95. }
  96. //读取订单
  97. $order = GreenOrder::where(['member_miniapp_id' => $this->miniapp_id,'user_id' =>$this->user->id,'shop_id' => $param['shop_id'],'is_del' => 0])->field('points,paid_at,order_no')->find();
  98. if(empty($order)){
  99. $order_no = $this->user->invite_code.order_no();
  100. $points = $item->points;
  101. $is_new_order = true;
  102. }else{
  103. if($order->points < $item->points){
  104. $order->is_del = 1;
  105. $order->save();
  106. return enjson(403,'订单已失效,请重新下单');
  107. }
  108. $order_no = $order->order_no;
  109. $points = $order->points;
  110. $is_new_order = false;
  111. }
  112. //唤醒微信支付参数
  113. $payparm = [
  114. 'openid' => $this->user->miniapp_uid,
  115. 'miniapp_id' => $this->miniapp_id,
  116. 'name' => $item->name,
  117. 'order_no' => $order_no,
  118. 'total_fee' => 1,
  119. 'notify_url' => api(1,'green/notify/shop',$this->miniapp_id),
  120. ];
  121. $ispay = WechatPay::orderPay($payparm);
  122. if ($ispay['code'] == 0) {
  123. return enjson(403,$ispay['msg']);
  124. }
  125. //判断是否新订单
  126. $rel = true;
  127. if($is_new_order){
  128. $param['member_miniapp_id'] = $this->miniapp_id;
  129. $param['points'] = $points;
  130. $param['user_id'] = $this->user->id;
  131. $param['shop_cache'] = $item->toJson();
  132. $param['express_name'] = $address['name'];
  133. $param['express_phone'] = $address['telphone'];
  134. $param['express_address'] = $address['address'];
  135. $rel = GreenOrder::insertOrder($param,$order_no);
  136. }
  137. if(empty($rel)){
  138. return enjson(204,'购买商品失败');
  139. }
  140. return enjson(200,'成功',$ispay['data']);
  141. }
  142. }
  143. //重新支付
  144. public function reDopay(){
  145. $this->isUserAuth();
  146. if (request()->isPost()) {
  147. $param['order_no'] = $this->request->param('order_no/s','');
  148. $param['signkey'] = $this->request->param('signkey');
  149. $param['sign'] = $this->request->param('sign');
  150. $sign = $this->apiSign($param);
  151. if($sign['code'] != 200){
  152. return enjson($sign['code'],'签名验证失败');
  153. }
  154. //读取订单
  155. $order = GreenOrder::where(['member_miniapp_id' => $this->miniapp_id,'user_id' =>$this->user->id,'order_no' => $param['order_no'],'paid_at'=>0,'is_del' => 0])->field('points,paid_at,order_no,shop_id')->find();
  156. if(empty($order)){
  157. return enjson(403,'订单已失效,请重新下单');
  158. }
  159. //判断是否已下架
  160. $item = GreenShop::where(['id' => $order->shop_id,'is_sale' => 1,'is_del' => 0])->field('id,points,name,note,img,imgs,content')->find();
  161. if(empty($item)){
  162. return enjson(403,'商品已经下架');
  163. }
  164. if($order->points < $item->points){
  165. $order->is_del = 1;
  166. $order->save();
  167. return enjson(403,'订单已失效,请重新下单');
  168. }
  169. //判断积分是否足够
  170. $bank = GreenUser::where(['member_miniapp_id'=>$this->miniapp_id,'uid' => $this->user->id])->find();
  171. if($bank->points < $item->points){
  172. return enjson(403,'积分不足够兑换');
  173. }
  174. //唤醒微信支付参数
  175. $payparm = [
  176. 'openid' => $this->user->miniapp_uid,
  177. 'miniapp_id' => $this->miniapp_id,
  178. 'name' => $item->name,
  179. 'order_no' => $order->order_no,
  180. 'total_fee' => 1,
  181. 'notify_url' => api(1,'green/notify/shop',$this->miniapp_id),
  182. ];
  183. $ispay = WechatPay::orderPay($payparm);
  184. if ($ispay['code'] == 0) {
  185. return enjson(403,$ispay['msg']);
  186. }
  187. return enjson(200,'成功',$ispay['data']);
  188. }
  189. }
  190. //立即兑换
  191. public function order(){
  192. $this->isUserAuth();
  193. $param['active'] = $this->request->param('active/d',0);
  194. $param['signkey'] = $this->request->param('signkey');
  195. $param['sign'] = $this->request->param('sign');
  196. $rel = $this->apiSign($param);
  197. if($rel['code'] != 200){
  198. return enjson($rel['code'],'签名验证失败');
  199. }
  200. $condition['user_id'] = $this->user->id;
  201. $condition['is_del'] = 0;
  202. switch ($param['active']) {
  203. case 1:
  204. $condition['paid_at'] = 0;
  205. break;
  206. case 2:
  207. $condition['paid_at'] = 1;
  208. $condition['express_status'] = 0;
  209. break;
  210. case 3:
  211. $condition['paid_at'] = 1;
  212. $condition['express_status'] = 1;
  213. break;
  214. }
  215. $order = GreenOrder::field('id,order_no,paid_at,paid_time,points,message,shop_id,express_status,shop_cache,amount,create_time,is_del')->where($condition)->paginate(10);
  216. if($order->isEmpty()){
  217. return enjson(204);
  218. }
  219. $data = [];
  220. $ids = [];
  221. foreach ($order as $key => $value) {
  222. $data[$key] = $value;
  223. $data[$key]['shop_cache'] = json_decode($value['shop_cache']);
  224. if($value['paid_at']){
  225. $data[$key]['status_text'] = $value['express_status']?'已发货':'待发货';
  226. $data[$key]['end_paytime'] = 0;
  227. }else{
  228. $data[$key]['status_text'] = '待支付';
  229. $data[$key]['end_paytime'] = (($value['create_time'] + 60 * 10) - time()) * 1000;
  230. }
  231. if($value['paid_at'] == 0 && $data[$key]['end_paytime'] < 0){
  232. $ids[] = $value['id'];
  233. unset($data[$key]);
  234. }
  235. }
  236. //过期了更改了状态
  237. if(!empty($ids)){
  238. GreenOrder::where(['id' => $ids])->update(['is_del' => 1]);
  239. }
  240. if(empty($data)){
  241. return enjson(204);
  242. }
  243. return enjson(200,'成功',array_values($data));
  244. }
  245. /**
  246. * @param int $store_id
  247. * @return \think\response\Json
  248. * @throws \think\db\exception\DataNotFoundException
  249. * @throws \think\db\exception\ModelNotFoundException
  250. * @throws \think\exception\DbException
  251. * 扫码店铺待核销订单列表
  252. */
  253. public function getOrder(){
  254. $this->isUserAuth();
  255. $param['id'] = $this->request->param('id/d',0);
  256. $param['signkey'] = $this->request->param('signkey');
  257. $param['sign'] = $this->request->param('sign');
  258. $rel = $this->apiSign($param);
  259. if($rel['code'] != 200){
  260. return enjson($rel['code'],'签名验证失败');
  261. }
  262. $condition['user_id'] = $this->user->id;
  263. $condition['id'] = $param['id'];
  264. $order = GreenOrder::where($condition)->find();
  265. if(empty($order)){
  266. return enjson(204);
  267. }
  268. $data = $order->toArray();
  269. $data['shop_cache'] = json_decode($order->shop_cache,true);
  270. if($data['paid_at']){
  271. $data['end_paytime'] = 0;
  272. }else{
  273. $data['end_paytime'] = (($order->create_time + 60 * 10) - time()) * 1000;
  274. }
  275. if($order->paid_at){
  276. $data['status_text'] = $order->express_status?'已发货':'待发货';
  277. $data['end_paytime'] = 0;
  278. }else{
  279. $data['status_text'] = '待支付';
  280. $data['end_paytime'] = (($order->create_time + 60 * 10) - time()) * 1000;
  281. }
  282. if($order->paid_at == 0 && $data['end_paytime'] < 0){
  283. $order->is_del = 1;
  284. $order->save();
  285. }
  286. $data['paid_time'] = date('Y-m-d H:i:s',$order->paid_time);
  287. $data['create_time'] = date('Y-m-d H:i:s',$order->create_time);
  288. return enjson(200,'成功',$data);
  289. }
  290. }