Pickupapi.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. namespace addons\pickup\controller;
  3. use app\common\controller\Api;
  4. use think\Cache;
  5. use think\Config;
  6. use think\Validate;
  7. use think\Db;
  8. use app\common\library\Sms as Smslib;
  9. /**
  10. * 微信接口
  11. */
  12. class Pickupapi extends Api {
  13. //class Index extends \think\addons\Controller {
  14. // 无需登录的接口,*表示全部
  15. protected $noNeedLogin = "*";
  16. // 无需鉴权的接口,*表示全部
  17. protected $noNeedRight = "*";
  18. protected $modelGiftPackage = null;
  19. protected $modelGiftCardcode = null;
  20. protected $modelGiftGoods = null;
  21. protected $modelGiftOrder = null;
  22. public function _initialize() {
  23. parent::_initialize();
  24. $this->modelGiftPackage = new \app\admin\model\pickup\Package;
  25. $this->modelGiftCardcode = new \app\admin\model\pickup\Cardcode;
  26. $this->modelGiftGoods = new \app\admin\model\pickup\Goods;
  27. $this->modelGiftOrder = new \app\admin\model\pickup\Order;
  28. }
  29. /**
  30. * 获取系统配置
  31. *
  32. * @ApiTitle (获取系统配置)
  33. * @ApiSummary (获取系统配置)
  34. * @ApiMethod (GET)
  35. * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
  36. * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
  37. * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
  38. * @ApiReturn ({
  39. 'code':'1',
  40. 'msg':'返回成功',
  41. 'data':[
  42. 'sitename':'',//站点名称
  43. 'beian':'',//站点备案
  44. 'cdnurl': '',//CDN地址
  45. 'captcha' : '',//验证码开关 1开启 0关闭
  46. ]
  47. })
  48. */
  49. public function config() {
  50. $Config = get_addon_config('pickup');
  51. $configCaptcha = $Config['captcha'];
  52. $data = [
  53. 'sitename' => Config::get('site.name'),
  54. 'beian' => Config::get('site.beian'),
  55. 'cdnurl' => Config::get('site.cdnurl'),
  56. // 'captcha' => Config::get('site.captcha'),
  57. 'captcha' => $Config['captcha'],
  58. 'bg' => $Config['bg'],
  59. ];
  60. $this->success('返回成功', $data);
  61. }
  62. /**
  63. * 使用兑换码兑换礼包
  64. *
  65. * @ApiTitle (使用兑换码兑换礼包)
  66. * @ApiSummary (使用兑换码兑换礼包)
  67. * @ApiMethod (GET)
  68. * @ApiParams (name="card_id", type="integer", required=true, description="卡号,卡号和密码在本地需要保存下,提交兑换的时候还需要,保障兑换功能的安全")
  69. * @ApiParams (name="card_pwd", type="integer", required=true, description="密码")
  70. * @ApiParams (name="code", type="integer", required=false, description="验证码")
  71. * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
  72. * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
  73. * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
  74. * @ApiReturn ({
  75. 'code':'1',
  76. 'msg':'返回成功'
  77. })
  78. */
  79. public function receiveGiftPackage() {
  80. $card_id = $this->request->request("card_id");
  81. $card_pwd = $this->request->request("card_pwd");
  82. $code = $this->request->request("code");
  83. // $configCaptcha = Config::get('site.captcha');
  84. $Config = get_addon_config('pickup');
  85. $configCaptcha = $Config['captcha'];
  86. if ($configCaptcha) {
  87. $validate = new Validate(['captcha' => 'require|captcha'], [], ['captcha' => __('验证码')]);
  88. $result = $validate->check(['captcha' => $code]);
  89. if (!$result) {
  90. $this->error($validate->getError());
  91. }
  92. }
  93. $ck_key = 'card_' . $card_id;
  94. $ck = Cache::get($ck_key, []);
  95. if ($ck && isset($ck['try']) && $ck['try'] >= $Config['try_num'] && $Config['try_num'] > 0) {
  96. $this->error("频繁输入错误,请稍后再试");
  97. }
  98. $giftCardcode = $this->modelGiftCardcode->get(['card_id' => $card_id, 'card_pwd' => $card_pwd]);
  99. if ($giftCardcode) {
  100. $giftCardcode = $giftCardcode->toArray();
  101. if ($giftCardcode['card_status'] == 1) {
  102. $this->error(__('卡号和密码信息不正确'));
  103. }
  104. if ($giftCardcode['card_status'] == 4) {
  105. $this->error(__('卡号和密码信息已失效'));
  106. }
  107. $this->success('返回成功', ['giftCardcode' => $giftCardcode]);
  108. } else {
  109. if ($ck && isset($ck['try'])) {
  110. $ck['try'] = $ck['try'] + 1;
  111. } else {
  112. $ck = [ 'try' => 1,];
  113. }
  114. Cache::set($ck_key, $ck, $Config['try_time']);
  115. $this->error(__('卡号和密码信息不正确'));
  116. }
  117. }
  118. /**
  119. * 获取礼包信息
  120. *
  121. * @ApiTitle (获取礼包信息)
  122. * @ApiSummary (获取礼包信息)
  123. * @ApiMethod (GET)
  124. * @ApiParams (name="id", type="integer", required=true, description="礼包ID")
  125. * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
  126. * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
  127. * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
  128. * @ApiReturn ({
  129. 'code':'1',
  130. 'msg':'返回成功'
  131. })
  132. */
  133. public function getGiftPackage() {
  134. $id = $this->request->request("id");
  135. $giftPackage = $this->modelGiftPackage->get(['id' => $id]);
  136. if ($giftPackage) {
  137. $giftPackage = $giftPackage->toArray();
  138. $goods_ids = json_decode($giftPackage['goods_list'], true);
  139. $goods_list = [];
  140. foreach ($goods_ids as $k => $v) {
  141. $goods = $this->modelGiftGoods->get($v);
  142. if ($goods) {
  143. $goods_list[] = $goods->toArray();
  144. }
  145. }
  146. $this->success('返回成功', ['gift' => $giftPackage, 'goods_list' => $goods_list]);
  147. } else {
  148. $this->error(__('礼包信息不存在'));
  149. }
  150. }
  151. /**
  152. * 提交兑换订单
  153. *
  154. * @ApiTitle (提交兑换订单)
  155. * @ApiSummary (提交兑换订单)
  156. * @ApiMethod (POST)
  157. * @ApiParams (name="gp_id", type="integer", required=true, description="礼包ID")
  158. * @ApiParams (name="gg_ids", type="string", required=true, description="选中的礼品ID,多个选中用“,”号连接")
  159. * @ApiParams (name="name", type="string", required=true, description="收件人")
  160. * @ApiParams (name="mobile", type="string", required=true, description="手机号")
  161. * @ApiParams (name="province_city", type="string", required=true, description="省市区")
  162. * @ApiParams (name="address", type="string", required=true, description="街道地址")
  163. * @ApiParams (name="ship_date_expected", type="string", required=true, description="预约发货日期")
  164. * @ApiParams (name="comments", type="string", required=true, description="留言")
  165. * @ApiParams (name="card_id", type="string", required=true, description="卡号")
  166. * @ApiParams (name="card_pwd", type="string", required=true, description="卡号密码")
  167. * @ApiParams (name="code", type="string", required=true, description="验证码")
  168. * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
  169. * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
  170. * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
  171. * @ApiReturn ({
  172. 'code':'1',
  173. 'msg':'返回成功'
  174. })
  175. */
  176. public function setGiftOrder() {
  177. $gp_id = intval($this->request->request("gp_id"));
  178. $gg_ids = $this->request->request("gg_ids");
  179. $name = $this->request->request("name");
  180. $mobile = $this->request->request("mobile");
  181. $province_city = $this->request->request("province_city");
  182. $address = $this->request->request("address");
  183. $ship_date_expected = $this->request->request("ship_date_expected");
  184. $comments = $this->request->request("comments");
  185. $card_id = $this->request->request("card_id");
  186. $card_pwd = $this->request->request("card_pwd");
  187. $code = $this->request->request("code");
  188. // $configCaptcha = Config::get('site.captcha');
  189. $Config = get_addon_config('pickup');
  190. $configCaptcha = $Config['captcha'];
  191. if ($configCaptcha) {
  192. $validate = new Validate(['captcha' => 'require|captcha'], [], ['captcha' => __('验证码')]);
  193. $result = $validate->check(['captcha' => $code]);
  194. if (!$result) {
  195. $this->error($validate->getError());
  196. }
  197. }
  198. $ck_key = 'card_' . $card_id;
  199. $ck = Cache::get($ck_key, []);
  200. if ($ck && isset($ck['try']) && $ck['try'] >= $Config['try_num'] && $Config['try_num'] > 0) {
  201. $this->error("频繁输入错误,请稍后再试");
  202. }
  203. $giftCardcode = $this->modelGiftCardcode->get(['card_id' => $card_id, 'card_pwd' => $card_pwd]);
  204. if ($giftCardcode) {
  205. // $giftCardcode = $giftCardcode->toArray();
  206. if ($giftCardcode['card_status'] == 1) {
  207. if ($ck && isset($ck['try'])) {
  208. $ck['try'] = $ck['try'] + 1;
  209. } else {
  210. $ck = [ 'try' => 1,];
  211. }
  212. Cache::set($ck_key, $ck, $Config['try_time']);
  213. $this->error(__('卡号和密码信息不正确'));
  214. }
  215. if ($giftCardcode['card_status'] == 4) {
  216. $this->error(__('卡号和密码信息已失效'));
  217. }
  218. if ($giftCardcode['card_status'] == 3) {
  219. $this->error(__('礼品卡已经兑换过了'), [], 202);
  220. }
  221. $giftPackage = $this->modelGiftPackage->get($giftCardcode['gp_id']);
  222. if ($giftPackage) {
  223. $nowTime = time();
  224. if ($giftPackage['start_time'] > $nowTime) {
  225. $this->error(__('礼品兑换活动尚未开始'));
  226. }
  227. if ($giftPackage['end_time'] < $nowTime) {
  228. $this->error(__('礼品兑换活动已经结束'));
  229. }
  230. } else {
  231. $this->error(__('礼品卡已经失效'));
  232. }
  233. $gg_ids_arr = explode(",", $gg_ids);
  234. $gg_list = [];
  235. $gg_list_title = [];
  236. $gg_list_image = [];
  237. foreach ($gg_ids_arr as $k => $v) {
  238. $modelGiftGoods = new \app\admin\model\pickup\Goods;
  239. $GiftGoods = $modelGiftGoods->get(['id' => $v]);
  240. if ($GiftGoods) {
  241. $gg_list_title[$v] = $GiftGoods['title'];
  242. $gg_list_image[$v] = $GiftGoods['image'];
  243. $gg_list[$v] = $GiftGoods->toArray();
  244. }
  245. }
  246. if (!$gg_list) {
  247. $this->error(__('请选择您要兑换的礼品'));
  248. }
  249. //在这里创建订单
  250. $result = false;
  251. Db::startTrans();
  252. try {
  253. $order_no = $this->onlyosn();
  254. $data = [
  255. "order_no" => $order_no,
  256. "gift_package_id" => $gp_id,
  257. "gift_package_title" => $giftPackage['title'],
  258. "gift_package_image" => $giftPackage['image'],
  259. "gift_goods_id" => $gg_ids,
  260. "gift_goods_title" => json_encode($gg_list_title),
  261. "gift_goods_image" => json_encode($gg_list_image),
  262. "gift_goods" => json_encode($gg_list),
  263. "name" => $name,
  264. "mobile" => $mobile,
  265. "province_city" => $province_city,
  266. "address" => $address,
  267. "ship_date_expected" => $ship_date_expected ? $ship_date_expected : "",
  268. "comments" => $comments,
  269. "card_id" => $card_id,
  270. "status" => 1,
  271. ];
  272. $this->modelGiftOrder->allowField(true)->save($data);
  273. $giftCardcode->card_status = 3;
  274. $giftCardcode->usedtime = time();
  275. $giftCardcode->save();
  276. $result = true;
  277. Db::commit();
  278. } catch (PDOException $e) {
  279. Db::rollback();
  280. $result = false;
  281. // $this->error($e->getMessage());
  282. $this->error(__('兑换失败'));
  283. } catch (Exception $e) {
  284. Db::rollback();
  285. $result = false;
  286. // $this->error($e->getMessage());
  287. $this->error(__('兑换失败'));
  288. }
  289. $this->success('提交成功', ['order_no' => $order_no]);
  290. } else {
  291. $this->error(__('卡号和密码信息不正确'));
  292. }
  293. $giftPackage = $this->modelGiftPackage->get(['id' => $id]);
  294. if ($giftPackage) {
  295. $giftPackage = $giftPackage->toArray();
  296. $goods_ids = json_decode($giftPackage['goods_list'], true);
  297. $goods_list = [];
  298. foreach ($goods_ids as $k => $v) {
  299. $goods = $this->modelGiftGoods->get($v);
  300. if ($goods) {
  301. $goods_list[] = $goods->toArray();
  302. }
  303. }
  304. $this->success('返回成功', ['gift' => $giftPackage, 'goods_list' => $goods_list]);
  305. } else {
  306. $this->error(__('礼包信息不存在'));
  307. }
  308. }
  309. /**
  310. * 获取快递信息
  311. *
  312. * @ApiTitle (获取快递信息)
  313. * @ApiSummary (获取快递信息)
  314. * @ApiMethod (POST)
  315. * @ApiParams (name="card_id", type="string", required=true, description="卡号")
  316. * @ApiParams (name="card_pwd", type="string", required=true, description="卡号密码")
  317. * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
  318. * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
  319. * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
  320. * @ApiReturn ({
  321. 'code':'1',
  322. 'msg':'返回成功'
  323. 'data':{
  324. status:1,//可能的值:1、2、3,1表示未发货,2表示有物流信息,3表示无物流信息
  325. list:[],//物流信息数组,未发货和无物流信息时,均提示:暂无物流信息,请隔段时间再查
  326. }
  327. })
  328. */
  329. public function getKuaidiInfo() {
  330. $card_id = $this->request->request("card_id");
  331. $card_pwd = $this->request->request("card_pwd");
  332. $giftCardcode = $this->modelGiftCardcode->get(['card_id' => $card_id, 'card_pwd' => $card_pwd]);
  333. if ($giftCardcode) {
  334. $giftCardcode = $giftCardcode->toArray();
  335. if ($giftCardcode['card_status'] == 1) {
  336. $this->error(__('卡号和密码信息不正确'));
  337. }
  338. if ($giftCardcode['card_status'] == 4) {
  339. $this->error(__('卡号和密码信息已失效'));
  340. }
  341. $giftOrder = $this->modelGiftOrder->get(['card_id' => $card_id,]);
  342. if ($giftOrder) {
  343. if ($giftOrder['status'] == 1) {
  344. //未发货
  345. $data = [[
  346. "time" => date("Y-m-d H:i:s"),
  347. "context" => "暂无物流信息,请隔段时间再查"
  348. ]];
  349. $status = 1;
  350. $kd_company = "";
  351. $kd_number = "";
  352. } elseif ($giftOrder['status'] == 2 || $giftOrder['status'] == 3) {
  353. //已发货
  354. $Kuaidi = \addons\pickup\library\Kuaidi100::getKdInfo([
  355. "com" => $giftOrder['kd_company'],
  356. "num" => $giftOrder['kd_number'],
  357. ]);
  358. if (isset($Kuaidi['status']) && $Kuaidi['status'] == 200) {
  359. $data = $Kuaidi['data'];
  360. $status = 2;
  361. } else {
  362. $data = [[
  363. "time" => date("Y-m-d H:i:s"),
  364. "context" => "暂无物流信息,请隔段时间再查"
  365. ]];
  366. $status = 3;
  367. }
  368. // $kd_company = Config::get('site.kd_company');
  369. $Config = get_addon_config('pickup');
  370. $kd_company = $Config['kd_company'];
  371. $kd_company_code = $giftOrder['kd_company'];
  372. if (isset($kd_company[$kd_company_code])) {
  373. $kd_company = $kd_company[$kd_company_code];
  374. } else {
  375. $kd_company = "其他快递";
  376. }
  377. $kd_number = $giftOrder['kd_number'];
  378. } else {
  379. $this->error(__('该卡号兑换订单不存在'));
  380. }
  381. $this->success('返回成功', ['status' => $status, 'list' => $data, "kd_company" => $kd_company, "kd_number" => $kd_number]);
  382. } else {
  383. $this->error(__('该卡号兑换订单不存在'));
  384. }
  385. } else {
  386. $this->error(__('卡号和密码信息不正确'));
  387. }
  388. }
  389. //通用生成唯一订单号
  390. private function onlyosn() {
  391. @date_default_timezone_set("PRC");
  392. $order_id_main = date('YmdHis') . rand(10000000, 99999999);
  393. //订单号码主体长度
  394. $order_id_len = strlen($order_id_main);
  395. $order_id_sum = 0;
  396. for ($i = 0; $i < $order_id_len; $i++) {
  397. $order_id_sum += (int) (substr($order_id_main, $i, 1));
  398. }
  399. //唯一订单号码(YYYYMMDDHHIISSNNNNNNNNCC)
  400. $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT); //生成唯一订单号
  401. return $osn;
  402. }
  403. }