Voucher.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace app\api\controller;
  3. use QRcode;
  4. use think\facade\Db;
  5. use think\facade\Lang;
  6. /**
  7. * ============================================================================
  8. * DSMall多用户商城
  9. * ============================================================================
  10. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  11. * 网站地址: http://www.csdeshang.com
  12. * ----------------------------------------------------------------------------
  13. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  14. * 不允许对程序代码以任何形式任何目的的再发布。
  15. * ============================================================================
  16. * 代金券控制器
  17. */
  18. class Voucher extends MobileMember {
  19. public function initialize() {
  20. parent::initialize();
  21. }
  22. /**
  23. * @api {POST} api/Voucher/voucher_tpl_list 代金券列表
  24. * @apiVersion 1.0.0
  25. * @apiGroup Voucher
  26. *
  27. * @apiParam {String} store_id 店铺ID
  28. * @apiParam {String} gettype 代金券类型
  29. *
  30. * @apiSuccess {String} code 返回码,10000为成功
  31. * @apiSuccess {String} message 返回消息
  32. * @apiSuccess {Object} result 返回数据
  33. * @apiSuccess {Object[]} result.voucher_list 代金券模板列表 (返回字段参考vouchertemplate)
  34. * @apiSuccess {String} result.voucher_list.vouchertemplate_enddate_text 代金券过期时间描述
  35. */
  36. public function voucher_tpl_list() {
  37. $param = array();
  38. $param['store_id']=input('param.store_id');
  39. $param['gettype']=input('param.gettype');
  40. $voucher_model = model('voucher');
  41. $templatestate_arr = $voucher_model->getTemplateState();
  42. $voucher_gettype_array = $voucher_model->getVoucherGettypeArray();
  43. $where = array();
  44. $where[]=array('vouchertemplate_state','=',$templatestate_arr['usable'][0]);
  45. $store_id = intval($param['store_id']);
  46. if ($store_id > 0) {
  47. $where[]=array('vouchertemplate_store_id','=',$store_id);
  48. }
  49. $where[] = array('vouchertemplate_gettype','in', array($voucher_gettype_array['points']['sign'], $voucher_gettype_array['free']['sign']));
  50. if ($param['gettype'] && in_array($param['gettype'], array('points', 'free'))) {
  51. $where[]=array('vouchertemplate_gettype','=',$voucher_gettype_array[$param['gettype']]['sign']);
  52. }
  53. $order = 'vouchertemplate_id asc';
  54. $voucher_list = $voucher_model->getVouchertemplateList($where, '*', 20, 0, $order);
  55. if ($voucher_list) {
  56. foreach ($voucher_list as $k => $v) {
  57. $v['vouchertemplate_limit'] = floatval($v['vouchertemplate_limit']);
  58. $v['vouchertemplate_enddate_text'] = $v['vouchertemplate_enddate'] ? @date('Y.m.d', $v['vouchertemplate_enddate']) : '';
  59. $voucher_list[$k] = $v;
  60. }
  61. }
  62. ds_json_encode(10000, '',array('voucher_list' => $voucher_list));
  63. }
  64. public function delete(){
  65. $id = input('param.id');
  66. $Vrordercode = Db::name('vrordercode');
  67. $voucher = $Vrordercode->where(['rec_id'=>$id])->find();
  68. if (!$voucher) {
  69. ds_json_encode(10001, '参数错误');
  70. }
  71. $result = $Vrordercode->where(['rec_id'=>$id])->update(['state'=>0]);
  72. if (!$result) {
  73. ds_json_encode(10001, '删除失败');
  74. }
  75. ds_json_encode(10000, '删除成功');
  76. }
  77. /**
  78. * ******************************以下内容为非框架部分******************************************
  79. */
  80. /**
  81. * 测试消费卡券绑定操作
  82. */
  83. public function addVoucher()
  84. {
  85. $param = ['a'=>1, 'b'=>2, 'c'=>3];
  86. $buy_logic = model('voucher','logic');
  87. $memberId = $this->member_info['member_id'];
  88. $result = $buy_logic->getVoucherListByGood($param,$memberId);
  89. echo json_encode($result);
  90. }
  91. /**
  92. * 消费卡券列表
  93. */
  94. public function voucherList()
  95. {
  96. $param = $this->request->param();
  97. $page = empty($param['page']) ? 1 : $param['page'];
  98. $limit = empty($param['limit']) ? 10 : $param['limit'];
  99. $memberId = $this->member_info['member_id'];
  100. // $userId = $this->member_info['member_id'];
  101. $where = [];
  102. $where[] = ['voc.buyer_id','=',$memberId];
  103. $model = Db::name('vrordercode');
  104. if(!empty($param['voucher_type']) && $param['voucher_type'] !=0){
  105. $where[] = ['type','=',$param['voucher_type']];
  106. }
  107. $time = time();
  108. $where[] = ['vr_end_time','>',$time];
  109. if(isset($param['state'])){
  110. $where[] = ['voc.state','=',$param['state']];
  111. }
  112. if(!empty($param['tag_id'])){
  113. //商品id不为空
  114. $where[] = ['voc.order_id','=',$param['tag_id']];
  115. $count = 0;
  116. $query = $model->where($where);
  117. }else{
  118. $page = empty($param['page']) ? 1 : $param['page'];
  119. $limit = empty($param['limit']) ? 10 : $param['limit'];
  120. $count = $model->count();
  121. $query = $model->where($where)->page($page)->limit($limit);
  122. }
  123. $query = $query->order('voc.rec_id desc');
  124. $query = $query->alias('voc')->join('Vrorder v','v.order_id = voc.order_id');
  125. $list = $query->field('voc.*,v.order_sn,v.add_time')->select()->toArray();
  126. // var_dump($query->getLastsql());exit();
  127. // del_code_tmp();//清除二维码临时文件
  128. // $data = '哈哈哈你猜';
  129. // $ewm = ewm($data);
  130. // echo $ewm;exit;
  131. if(!empty($list)){
  132. foreach($list as $k=>$v){
  133. switch ($v['type']){
  134. case 1:
  135. $list[$k]['type_name'] = '卡号';
  136. break;
  137. case 2:
  138. $list[$k]['type_name'] = '卡密';
  139. break;
  140. case 3:
  141. $list[$k]['type_name'] = '二维码';
  142. //二维码
  143. // $this->del_code_tmp();//清除二维码临时文件
  144. // $ewm = $this->ewm($v['content']);
  145. // $list[$k]['qrcode_url'] = $ewm;
  146. break;
  147. case 4:
  148. $list[$k]['type_name'] = '条形码';
  149. break;
  150. case 5:
  151. $list[$k]['type_name'] = '动态码';
  152. break;
  153. }
  154. $list[$k]['expire_time'] = date('Y-m-d',$v['vr_end_time']);
  155. // $list[$k]['create_time'] = date('Y-m-d',$v['create_time']);
  156. $list[$k]['start_time'] = date('Y-m-d',$v['vr_start_time']);
  157. $list[$k]['add_time'] = date('Y-m-d',$v['add_time']);
  158. }
  159. }
  160. if (count($list) < $limit) {
  161. $hasMore = false;
  162. } else {
  163. $hasMore = true;
  164. }
  165. $data = [
  166. 'count' => $count,
  167. 'list' => $list,
  168. 'hasmore' => $hasMore
  169. ];
  170. ds_json_encode(10000, '查询成功', $data);
  171. }
  172. //生成二维码
  173. //$type真输出,假返回地址
  174. function ewm ($text,$type=true){
  175. $nums=time().'_'.mt_rand();
  176. //当前时间戳加随机数
  177. $size='6';
  178. $level='H';
  179. $padding=2;
  180. $root = $_SERVER['DOCUMENT_ROOT'];
  181. $QR=$root.'/code/tmp/'.$nums.'.png';
  182. include_once root_path(). 'extend/qrcode/phpqrcode.php';
  183. $re=\QRcode::png ($text,$QR,$level,$size,$padding);
  184. if ($type){
  185. // ob_end_clean();
  186. $domain = \think\facade\Request::domain();
  187. $abc = $domain.'/code/tmp/'.$nums.'.png';
  188. return $abc;
  189. }else {
  190. return $QR;
  191. }
  192. }
  193. //删除临时二维码
  194. function del_code_tmp (){
  195. //删除时间戳大于1分钟的内容
  196. $hostdir= 'code/tmp/';
  197. //获取文件目录的文件夹地址
  198. $filesnames=scandir($hostdir);
  199. foreach ($filesnames as $name){
  200. if (strstr($name,"_")){
  201. $tmp=explode("_",$name);
  202. if (time()-$tmp[0]>3600){
  203. //如果拆分文件名时间戳大于当前时间30秒则删除
  204. unlink($hostdir.$name);
  205. }
  206. }
  207. }
  208. }
  209. /**
  210. * 消费卡券详情
  211. */
  212. public function voucherInfo()
  213. {
  214. $param = $this->request->param();
  215. if(empty($param['id'])){
  216. ds_json_encode(10001, '参数丢失!');
  217. }
  218. $model = Db::name('sub_voucher');
  219. $memberId = $this->member_info['member_id'];
  220. $info = $model->where([
  221. ['member_id','=',$memberId],
  222. ['id','=',$param['id']]
  223. ])->find();
  224. $time = time();
  225. if(empty($info) || $info['expire_time'] < $time){
  226. ds_json_encode(10001, '卡券不存在或已过期!');
  227. }
  228. switch ($info['voucher_type']){
  229. case 1:
  230. $info['data_info'] = json_decode($info['content'],true);
  231. break;
  232. case 2:
  233. $info['data_info'] = json_decode($info['content'],true);
  234. break;
  235. case 3:
  236. //二维码
  237. $this->del_code_tmp();//清除二维码临时文件
  238. $ewm = $this->ewm($info['content']);
  239. $info['qrcode_url'] = $ewm;
  240. break;
  241. }
  242. $info['expire_time'] = date('Y-m-d H:i:s',$info['expire_time']);
  243. $info['start_time'] = date('Y-m-d H:i:s',$info['start_time']);
  244. ds_json_encode(10000, '查询成功', ['info' => $info]);
  245. }
  246. }
  247. ?>