| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <?php
- namespace addons\pickup\controller;
- use app\common\controller\Api;
- use think\Cache;
- use think\Config;
- use think\Validate;
- use think\Db;
- use app\common\library\Sms as Smslib;
- /**
- * 微信接口
- */
- class Pickupapi extends Api {
- //class Index extends \think\addons\Controller {
- // 无需登录的接口,*表示全部
- protected $noNeedLogin = "*";
- // 无需鉴权的接口,*表示全部
- protected $noNeedRight = "*";
- protected $modelGiftPackage = null;
- protected $modelGiftCardcode = null;
- protected $modelGiftGoods = null;
- protected $modelGiftOrder = null;
- public function _initialize() {
- parent::_initialize();
- $this->modelGiftPackage = new \app\admin\model\pickup\Package;
- $this->modelGiftCardcode = new \app\admin\model\pickup\Cardcode;
- $this->modelGiftGoods = new \app\admin\model\pickup\Goods;
- $this->modelGiftOrder = new \app\admin\model\pickup\Order;
- }
- /**
- * 获取系统配置
- *
- * @ApiTitle (获取系统配置)
- * @ApiSummary (获取系统配置)
- * @ApiMethod (GET)
- * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
- * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
- * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
- * @ApiReturn ({
- 'code':'1',
- 'msg':'返回成功',
- 'data':[
- 'sitename':'',//站点名称
- 'beian':'',//站点备案
- 'cdnurl': '',//CDN地址
- 'captcha' : '',//验证码开关 1开启 0关闭
- ]
- })
- */
- public function config() {
- $Config = get_addon_config('pickup');
- $configCaptcha = $Config['captcha'];
- $data = [
- 'sitename' => Config::get('site.name'),
- 'beian' => Config::get('site.beian'),
- 'cdnurl' => Config::get('site.cdnurl'),
- // 'captcha' => Config::get('site.captcha'),
- 'captcha' => $Config['captcha'],
- 'bg' => $Config['bg'],
- ];
- $this->success('返回成功', $data);
- }
- /**
- * 使用兑换码兑换礼包
- *
- * @ApiTitle (使用兑换码兑换礼包)
- * @ApiSummary (使用兑换码兑换礼包)
- * @ApiMethod (GET)
- * @ApiParams (name="card_id", type="integer", required=true, description="卡号,卡号和密码在本地需要保存下,提交兑换的时候还需要,保障兑换功能的安全")
- * @ApiParams (name="card_pwd", type="integer", required=true, description="密码")
- * @ApiParams (name="code", type="integer", required=false, description="验证码")
- * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
- * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
- * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
- * @ApiReturn ({
- 'code':'1',
- 'msg':'返回成功'
- })
- */
- public function receiveGiftPackage() {
- $card_id = $this->request->request("card_id");
- $card_pwd = $this->request->request("card_pwd");
- $code = $this->request->request("code");
- // $configCaptcha = Config::get('site.captcha');
- $Config = get_addon_config('pickup');
- $configCaptcha = $Config['captcha'];
- if ($configCaptcha) {
- $validate = new Validate(['captcha' => 'require|captcha'], [], ['captcha' => __('验证码')]);
- $result = $validate->check(['captcha' => $code]);
- if (!$result) {
- $this->error($validate->getError());
- }
- }
-
- $ck_key = 'card_' . $card_id;
- $ck = Cache::get($ck_key, []);
- if ($ck && isset($ck['try']) && $ck['try'] >= $Config['try_num'] && $Config['try_num'] > 0) {
- $this->error("频繁输入错误,请稍后再试");
- }
- $giftCardcode = $this->modelGiftCardcode->get(['card_id' => $card_id, 'card_pwd' => $card_pwd]);
- if ($giftCardcode) {
- $giftCardcode = $giftCardcode->toArray();
- if ($giftCardcode['card_status'] == 1) {
- $this->error(__('卡号和密码信息不正确'));
- }
- if ($giftCardcode['card_status'] == 4) {
- $this->error(__('卡号和密码信息已失效'));
- }
- $this->success('返回成功', ['giftCardcode' => $giftCardcode]);
- } else {
- if ($ck && isset($ck['try'])) {
- $ck['try'] = $ck['try'] + 1;
- } else {
- $ck = [ 'try' => 1,];
- }
- Cache::set($ck_key, $ck, $Config['try_time']);
- $this->error(__('卡号和密码信息不正确'));
- }
- }
- /**
- * 获取礼包信息
- *
- * @ApiTitle (获取礼包信息)
- * @ApiSummary (获取礼包信息)
- * @ApiMethod (GET)
- * @ApiParams (name="id", type="integer", required=true, description="礼包ID")
- * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
- * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
- * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
- * @ApiReturn ({
- 'code':'1',
- 'msg':'返回成功'
- })
- */
- public function getGiftPackage() {
- $id = $this->request->request("id");
- $giftPackage = $this->modelGiftPackage->get(['id' => $id]);
- if ($giftPackage) {
- $giftPackage = $giftPackage->toArray();
- $goods_ids = json_decode($giftPackage['goods_list'], true);
- $goods_list = [];
- foreach ($goods_ids as $k => $v) {
- $goods = $this->modelGiftGoods->get($v);
- if ($goods) {
- $goods_list[] = $goods->toArray();
- }
- }
- $this->success('返回成功', ['gift' => $giftPackage, 'goods_list' => $goods_list]);
- } else {
- $this->error(__('礼包信息不存在'));
- }
- }
- /**
- * 提交兑换订单
- *
- * @ApiTitle (提交兑换订单)
- * @ApiSummary (提交兑换订单)
- * @ApiMethod (POST)
- * @ApiParams (name="gp_id", type="integer", required=true, description="礼包ID")
- * @ApiParams (name="gg_ids", type="string", required=true, description="选中的礼品ID,多个选中用“,”号连接")
- * @ApiParams (name="name", type="string", required=true, description="收件人")
- * @ApiParams (name="mobile", type="string", required=true, description="手机号")
- * @ApiParams (name="province_city", type="string", required=true, description="省市区")
- * @ApiParams (name="address", type="string", required=true, description="街道地址")
- * @ApiParams (name="ship_date_expected", type="string", required=true, description="预约发货日期")
- * @ApiParams (name="comments", type="string", required=true, description="留言")
- * @ApiParams (name="card_id", type="string", required=true, description="卡号")
- * @ApiParams (name="card_pwd", type="string", required=true, description="卡号密码")
- * @ApiParams (name="code", type="string", required=true, description="验证码")
- * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
- * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
- * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
- * @ApiReturn ({
- 'code':'1',
- 'msg':'返回成功'
- })
- */
- public function setGiftOrder() {
- $gp_id = intval($this->request->request("gp_id"));
- $gg_ids = $this->request->request("gg_ids");
- $name = $this->request->request("name");
- $mobile = $this->request->request("mobile");
- $province_city = $this->request->request("province_city");
- $address = $this->request->request("address");
- $ship_date_expected = $this->request->request("ship_date_expected");
- $comments = $this->request->request("comments");
- $card_id = $this->request->request("card_id");
- $card_pwd = $this->request->request("card_pwd");
- $code = $this->request->request("code");
- // $configCaptcha = Config::get('site.captcha');
- $Config = get_addon_config('pickup');
- $configCaptcha = $Config['captcha'];
- if ($configCaptcha) {
- $validate = new Validate(['captcha' => 'require|captcha'], [], ['captcha' => __('验证码')]);
- $result = $validate->check(['captcha' => $code]);
- if (!$result) {
- $this->error($validate->getError());
- }
- }
-
- $ck_key = 'card_' . $card_id;
- $ck = Cache::get($ck_key, []);
- if ($ck && isset($ck['try']) && $ck['try'] >= $Config['try_num'] && $Config['try_num'] > 0) {
- $this->error("频繁输入错误,请稍后再试");
- }
- $giftCardcode = $this->modelGiftCardcode->get(['card_id' => $card_id, 'card_pwd' => $card_pwd]);
- if ($giftCardcode) {
- // $giftCardcode = $giftCardcode->toArray();
- if ($giftCardcode['card_status'] == 1) {
- if ($ck && isset($ck['try'])) {
- $ck['try'] = $ck['try'] + 1;
- } else {
- $ck = [ 'try' => 1,];
- }
- Cache::set($ck_key, $ck, $Config['try_time']);
- $this->error(__('卡号和密码信息不正确'));
- }
- if ($giftCardcode['card_status'] == 4) {
- $this->error(__('卡号和密码信息已失效'));
- }
- if ($giftCardcode['card_status'] == 3) {
- $this->error(__('礼品卡已经兑换过了'), [], 202);
- }
- $giftPackage = $this->modelGiftPackage->get($giftCardcode['gp_id']);
- if ($giftPackage) {
- $nowTime = time();
- if ($giftPackage['start_time'] > $nowTime) {
- $this->error(__('礼品兑换活动尚未开始'));
- }
- if ($giftPackage['end_time'] < $nowTime) {
- $this->error(__('礼品兑换活动已经结束'));
- }
- } else {
- $this->error(__('礼品卡已经失效'));
- }
- $gg_ids_arr = explode(",", $gg_ids);
- $gg_list = [];
- $gg_list_title = [];
- $gg_list_image = [];
- foreach ($gg_ids_arr as $k => $v) {
- $modelGiftGoods = new \app\admin\model\pickup\Goods;
- $GiftGoods = $modelGiftGoods->get(['id' => $v]);
- if ($GiftGoods) {
- $gg_list_title[$v] = $GiftGoods['title'];
- $gg_list_image[$v] = $GiftGoods['image'];
- $gg_list[$v] = $GiftGoods->toArray();
- }
- }
- if (!$gg_list) {
- $this->error(__('请选择您要兑换的礼品'));
- }
- //在这里创建订单
- $result = false;
- Db::startTrans();
- try {
- $order_no = $this->onlyosn();
- $data = [
- "order_no" => $order_no,
- "gift_package_id" => $gp_id,
- "gift_package_title" => $giftPackage['title'],
- "gift_package_image" => $giftPackage['image'],
- "gift_goods_id" => $gg_ids,
- "gift_goods_title" => json_encode($gg_list_title),
- "gift_goods_image" => json_encode($gg_list_image),
- "gift_goods" => json_encode($gg_list),
- "name" => $name,
- "mobile" => $mobile,
- "province_city" => $province_city,
- "address" => $address,
- "ship_date_expected" => $ship_date_expected ? $ship_date_expected : "",
- "comments" => $comments,
- "card_id" => $card_id,
- "status" => 1,
- ];
- $this->modelGiftOrder->allowField(true)->save($data);
- $giftCardcode->card_status = 3;
- $giftCardcode->usedtime = time();
- $giftCardcode->save();
- $result = true;
- Db::commit();
- } catch (PDOException $e) {
- Db::rollback();
- $result = false;
- // $this->error($e->getMessage());
- $this->error(__('兑换失败'));
- } catch (Exception $e) {
- Db::rollback();
- $result = false;
- // $this->error($e->getMessage());
- $this->error(__('兑换失败'));
- }
- $this->success('提交成功', ['order_no' => $order_no]);
- } else {
- $this->error(__('卡号和密码信息不正确'));
- }
- $giftPackage = $this->modelGiftPackage->get(['id' => $id]);
- if ($giftPackage) {
- $giftPackage = $giftPackage->toArray();
- $goods_ids = json_decode($giftPackage['goods_list'], true);
- $goods_list = [];
- foreach ($goods_ids as $k => $v) {
- $goods = $this->modelGiftGoods->get($v);
- if ($goods) {
- $goods_list[] = $goods->toArray();
- }
- }
- $this->success('返回成功', ['gift' => $giftPackage, 'goods_list' => $goods_list]);
- } else {
- $this->error(__('礼包信息不存在'));
- }
- }
- /**
- * 获取快递信息
- *
- * @ApiTitle (获取快递信息)
- * @ApiSummary (获取快递信息)
- * @ApiMethod (POST)
- * @ApiParams (name="card_id", type="string", required=true, description="卡号")
- * @ApiParams (name="card_pwd", type="string", required=true, description="卡号密码")
- * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
- * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
- * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
- * @ApiReturn ({
- 'code':'1',
- 'msg':'返回成功'
- 'data':{
- status:1,//可能的值:1、2、3,1表示未发货,2表示有物流信息,3表示无物流信息
- list:[],//物流信息数组,未发货和无物流信息时,均提示:暂无物流信息,请隔段时间再查
- }
- })
- */
- public function getKuaidiInfo() {
- $card_id = $this->request->request("card_id");
- $card_pwd = $this->request->request("card_pwd");
- $giftCardcode = $this->modelGiftCardcode->get(['card_id' => $card_id, 'card_pwd' => $card_pwd]);
- if ($giftCardcode) {
- $giftCardcode = $giftCardcode->toArray();
- if ($giftCardcode['card_status'] == 1) {
- $this->error(__('卡号和密码信息不正确'));
- }
- if ($giftCardcode['card_status'] == 4) {
- $this->error(__('卡号和密码信息已失效'));
- }
- $giftOrder = $this->modelGiftOrder->get(['card_id' => $card_id,]);
- if ($giftOrder) {
- if ($giftOrder['status'] == 1) {
- //未发货
- $data = [[
- "time" => date("Y-m-d H:i:s"),
- "context" => "暂无物流信息,请隔段时间再查"
- ]];
- $status = 1;
- $kd_company = "";
- $kd_number = "";
- } elseif ($giftOrder['status'] == 2 || $giftOrder['status'] == 3) {
- //已发货
- $Kuaidi = \addons\pickup\library\Kuaidi100::getKdInfo([
- "com" => $giftOrder['kd_company'],
- "num" => $giftOrder['kd_number'],
- ]);
- if (isset($Kuaidi['status']) && $Kuaidi['status'] == 200) {
- $data = $Kuaidi['data'];
- $status = 2;
- } else {
- $data = [[
- "time" => date("Y-m-d H:i:s"),
- "context" => "暂无物流信息,请隔段时间再查"
- ]];
- $status = 3;
- }
- // $kd_company = Config::get('site.kd_company');
- $Config = get_addon_config('pickup');
- $kd_company = $Config['kd_company'];
- $kd_company_code = $giftOrder['kd_company'];
- if (isset($kd_company[$kd_company_code])) {
- $kd_company = $kd_company[$kd_company_code];
- } else {
- $kd_company = "其他快递";
- }
- $kd_number = $giftOrder['kd_number'];
- } else {
- $this->error(__('该卡号兑换订单不存在'));
- }
- $this->success('返回成功', ['status' => $status, 'list' => $data, "kd_company" => $kd_company, "kd_number" => $kd_number]);
- } else {
- $this->error(__('该卡号兑换订单不存在'));
- }
- } else {
- $this->error(__('卡号和密码信息不正确'));
- }
- }
- //通用生成唯一订单号
- private function onlyosn() {
- @date_default_timezone_set("PRC");
- $order_id_main = date('YmdHis') . rand(10000000, 99999999);
- //订单号码主体长度
- $order_id_len = strlen($order_id_main);
- $order_id_sum = 0;
- for ($i = 0; $i < $order_id_len; $i++) {
- $order_id_sum += (int) (substr($order_id_main, $i, 1));
- }
- //唯一订单号码(YYYYMMDDHHIISSNNNNNNNNCC)
- $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT); //生成唯一订单号
- return $osn;
- }
- }
|